• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# Discord Bot to announnce joining specific voice channels to specific text channel, have the basic bot setup and tested working no problems! "C#"

Irish19

New Coder
"Hi sorry i have looked and searched on this website aswell but i just cant find the solution, i am coding in C# and am used to pythoon but it stopped working for like 2 of my bots, and i am trying to remake it on this one as it has always run but not a coding language i am used to, if u could pls let me know how that would be appritiated!, Im using the discord bot api linked here https://github.com/discord-net/Discord.Net"

C#:
using System;

using System.Collections.Generic;

using System.Linq;

using System.Reflection;

using System.Text;

using System.Threading.Tasks;

using Discord;

using Discord.Commands;

using Discord.WebSocket;

using Microsoft.Extensions.DependencyInjection;



namespace Discord_bot

{

    class Program

    {

        static void Main(string[] args) => new Program().RunBotAsync().GetAwaiter().GetResult();



        private DiscordSocketClient _client;

        private CommandService _commands;

        private IServiceProvider _services;



        public async Task RunBotAsync()

        {

            _client = new DiscordSocketClient();

            _commands = new CommandService();



            _services = new ServiceCollection()

                 .AddSingleton(_client)

                 .AddSingleton(_commands)

                 .BuildServiceProvider();



            String token = "MyBotToken";



            _client.Log += _client_Log;



            await RegisterCommandsAsync();



            await _client.LoginAsync(TokenType.Bot, token);



            await _client.StartAsync();



            await Task.Delay(-1);

        }



        private Task _client_Log(LogMessage arg)

        {

            Console.WriteLine(arg);

            return Task.CompletedTask;

        }



        public async Task RegisterCommandsAsync()

        {

            _client.MessageReceived += HandlCommandAsync;

            await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);

        }



        private async Task HandlCommandAsync(SocketMessage arg)

        {

            var message = arg as SocketUserMessage;

            var context = new SocketCommandContext(_client, message);

            if (message.Author.IsBot) return;



            int argPos = 0;

            if (message.HasStringPrefix("!", ref argPos))

            {

                var result = await _commands.ExecuteAsync(context, argPos, _services);

                if (!result.IsSuccess) Console.WriteLine(result.ErrorReason);

            }

        }

    }

}
 
Last edited by a moderator:

Malcolm

Administrator
Administrator
Staff Team
Hey there,

So just wanted to confirm my understanding, you got the bot to work but the next step is making it announce a message when joining specific channels. Is that correct?
 

Jenner

Coder
I've done Discord BOT programming in .NET using Discord.NET, though my preferred programming language for .NET is Visual Basic. I can post some code and maybe convert it into C# since the languages have identical functionality but as Malcolm says, what "exactly" is "working" and "not working"?

Is your code connecting with Discord's services and not returning an error when you create the client and start it?
Is it registering commands?
Is it posting back to channels?
 

Antero360

King Coder
Staff Team
Security Analyst
I primarily work with C#.NET, and have done several bots with the Discord.NET library. Does your bot give off an error message when it fails?
Taking a look at the code you provided.. I don't see a call to _client.Ready, which sets up any functionality you code in there to run when the client is connected and ready. I suggest you add that for ease of debugging. Below is a screencap of how I set up the RunAsync and Ready functions for all my bots, hasn't failed me... yet XD

1659252598771.png
 
Last edited:

Jenner

Coder
I primarily work with C#.NET, and have done several bots with the Discord.NET library. Does your bot give off an error message when it fails?
Taking a look at the code you provided.. I don't see a call to _client.Ready, which sets up any functionality you code in there to run when the client is connected and ready. I suggest you add that for ease of debugging. Below is a screencap of how I set up the RunAsync and Ready functions for all my bots, hasn't failed me... yet XD

View attachment 1540
I like that. That's very similar to how I have it set up with Visual Basic as a codebase. It has some cleaner things to it so I may grab parts of it. I think the only thing I do additionally is create a DiscordSockeyConfig so I can enable some GatewayIntents to allow my bot to detect the roles of who is sending it commands. That way I can have it do both User and Admin style commands.
 

Antero360

King Coder
Staff Team
Security Analyst
I like that. That's very similar to how I have it set up with Visual Basic as a codebase. It has some cleaner things to it so I may grab parts of it. I think the only thing I do additionally is create a DiscordSockeyConfig so I can enable some GatewayIntents to allow my bot to detect the roles of who is sending it commands. That way I can have it do both User and Admin style commands.
Go for it :) matter of fact, if you'd like, I can send you the shell of one of the bots I have made, and you can customize it as you wish
 

Jenner

Coder
Go for it :) matter of fact, if you'd like, I can send you the shell of one of the bots I have made, and you can customize it as you wish
Thank you! I'd appreciate that! My biggest headache has always been the initialization and shutdown of these bots. Getting it to launch cleanly and connect and able to terminate cleanly on command, which I do via a boolean that kills a keep-alive and processing thread and then joins everything at the end.
 
Top