Programing a Discord Bot! (Need an uptodate tutorial)

Updated on May 1, 2017 in Tutorial Ideas
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
3 on April 29, 2017

Hello everyone!

I’m trying to make a Discord Bot. I search for some C# tutorials for this, but they where all outdated (Discord.NET).

So I’m here to ask if someone can link a good (updated) Tutorial for beginners, or maybe make one (as i saw, the basics of a Discord bot is very simple).

What i want is to understand how to make the bot Connect to Discord server, and recognize commands.
Commands such as playing and queuing music (from youtube?) or assign Roles for the users.

The idea is not only to make a bot, but to learn how and why. So please don’t just paste the code down there.

If you’re going to explain it to me, you’re awesome. But explain in a smart way, explain what each thing is, in a way i (or anyone) can google it if don’t understand. (I know I’m asking too much, but i really want to learn!)
I prefer it to be in C# or nodeJS, but feel free to show me a new language.

Thanks for the attention given to me! =P
Sorry for any english mistakes.

Edit :
Some useful stuff :
– This documentation works fine (even if says 0.94): http://rtd.discord.foxbot.me/en/legacy/index.html
– First Steps to make a bot (note that its outdate, but is still useful. Just add “, TokenType.Bot” after the token): https://www.youtube.com/watch?v=ey8woPqvRal 

– Discord.NET 1.0 Doc: https://discord.foxbot.me/docs/

New code (01/05/2017) :
I’ll keep it updated as i add new commands.
I ask if someone can read it and look for any mistakes i could have made. Or show me command examples.

using Discord;
using Discord.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DiscordBot
{
    class Program
    {
        static void Main(string[] args) => new Program().Start();
        private DiscordClient _client;
        public void Start()
        {
            _client = new DiscordClient(x =>
            {
                x.AppName = " Discord Bot ";
                x.LogLevel = LogSeverity.Info;
                x.LogHandler = Log;
            });
            //set bot prefix
            _client.UsingCommands(x => {
                x.PrefixChar = '~'; //find how to set 2 character as prefix ";/"
                x.AllowMentionPrefix = true;
                x.HelpMode = HelpMode.Public;
            });
//            //Echo Bot! ... i think
//            _client.MessageReceived += async (s, e) =>
//            {
//                if (!e.Message.IsAuthor)
//                    await e.Channel.SendMessage(e.Message.Text);
//            };
            CreatCommands();
            var token = "-x-x-x-";
            _client.ExecuteAndWait(async () =>
            {
                await _client.Connect(token, TokenType.Bot);
            });
        }
        public void CreatCommands()
        {
            //this is not working! - TODO: Learn About Aliases
            _client.GetService().CreateCommand("name") //Create a command
                .Alias(new string[] { "cmd", "command" }) //add 2 aliases, so it can be run with ~cmd and ~command
                .Description("Command description.") //add description, it will be shown when ~help is used
                .Parameter("ParameterName", ParameterType.Required) //as an argument, we have a person we want to greet
                .Do(async e =>
                {
                    await e.Channel.SendMessage($"{e.User.Name} Command reply {e.GetArg("ParameterName")}"); //sends a message to channel with the given text
                });
            //Ping test - TODO: Find a way to test ping
            _client.GetService().CreateCommand("ping")
                .Description("Returns 'pong'.")
                .Do(async (e) =>
                {
                    await e.Channel.SendMessage("pong");
                });
            //Say
            _client.GetService().CreateCommand("say")
                .Description("Makes the bot says something.")
                .Parameter("phrase", ParameterType.Unparsed)
                .Do (async (e) =>
                 {
                     var reply = $"{e.GetArg("phrase")}";
                     await e.Channel.SendMessage(reply);
                 });
            //also not working - TODO: Find how to create a Channel, learn more about Parameters and make this work!
            _client.GetService().CreateGroup("make", make =>
            {
                make.CreateCommand("textch") //~make textch
                .Alias(new string[] { "textch" })
                .Description("Creates a new Text Channel.")// Not yet
                .Parameter("???", ParameterType.Required)
                .Do(async e =>
                {
                    await e.Channel.SendMessage($"{e.User.Name} i donno wtf i'm doing {e.GetArg("???")}");
                });
                make.CreateCommand("voicech") //~make voicech
                .Alias(new string[] { "voicech" })
                .Description("Creates a new Voice Channel.") // still, not yet
                .Parameter("????", ParameterType.Required)
                .Do(async e =>
                {
                    await e.Channel.SendMessage($"{e.User.Name} change it later {e.GetArg("???")}");
                });
            });
            //exemple (even the exemple doesn't work) //we would run our commands with ~do greet X and ~do bye X
            _client.GetService().CreateGroup("do", cgb =>
            {
                cgb.CreateCommand("greet")
                        .Alias(new string[] { "gr", "hi" })
                        .Description("Greets a person.")
                        .Parameter("GreetedPerson", ParameterType.Required)
                        .Do(async e =>
                        {
                            await e.Channel.SendMessage($"{e.User.Name} greets {e.GetArg("GreetedPerson")}");
                        });
                cgb.CreateCommand("bye")
                        .Alias(new string[] { "bb", "gb" })
                        .Description("Greets a person.")
                        .Parameter("GreetedPerson", ParameterType.Required)
                        .Do(async e =>
                        {
                            await e.Channel.SendMessage($"{e.User.Name} says goodbye to {e.GetArg("GreetedPerson")}");
                        });
            });
        }
        public void Log(object sender, LogMessageEventArgs e)
        {
            Console.WriteLine($"[{e.Severity}] [{e.Source}] {e.Message}");
        }
    }
}
  • Liked by
Reply
1 on April 30, 2017

Read trough the documentation of Discord.Net you should be able to get pretty far with that.

on April 30, 2017

O found this: http://rtd.discord.foxbot.me/en/legacy/index.html

But it says it’s version 0.94. The latest is the 0.96. (i will still read it)

I joined they Discord chat and i’ll ask for help there too.
/
/
Edit :According to they discord chat, this should be uptodate, since it is  v0.9x and wont be further updated. But i believe its some small changes.

Show more replies
  • Liked by
Reply
Cancel
0 on May 1, 2017

I made my way around with that documentation. And i made this :

C#

using Discord;
using Discord.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DiscordBot
{
    class Program
    {
        static void Main(string[] args) => new Program().Start();
        private DiscordClient _client;
        public void Start()
        {
            _client = new DiscordClient();
            _client.MessageReceived += async (s, e) =>
            {
                if (!e.Message.IsAuthor)
                    await e.Channel.SendMessage(e.Message.Text);
            };
            _client.ExecuteAndWait(async () => {
                await _client.Connect("MzB4tDI3MjYxMTQ2MjM0ODgw.C-f7yg.odsFzgy0ZISJdp0-d2-ebEmAz5Y", TokenType.Bot);
            });
            //set bot prefix
            _client.UsingCommands(x => {
                x.PrefixChar = '~'; //find how to set 2 character as prefix ";/"
                x.HelpMode = HelpMode.Public;
            });
            
            _client.GetService<CommandService>().CreateCommand("name") //Create a command
                .Alias(new string[] { "cmd", "command" }) //add 2 aliases, so it can be run with ~cmd and ~command
                .Description("Command description.") //add description, it will be shown when ~help is used
                .Parameter("ParameterName", ParameterType.Required) //as an argument, we have a person we want to greet
                .Do(async e =>
                {
                    await e.Channel.SendMessage($"{e.User.Name} Command reply {e.GetArg("ParameterName")}"); //sends a message to channel with the given text
                });
            //TODO: Find how to create a Channel, learn more about Parameters and make this work!
            _client.GetService<CommandService>().CreateGroup("make", make =>
            {
                make.CreateCommand("textch") //~make textch
                .Alias(new string[] { "textch" })
                .Description("Creates a new Text Channel.")// Not work yet
                .Parameter("???", ParameterType.Required)
                .Do(async e =>
               {
                   await e.Channel.SendMessage($"{e.User.Name} i donno wtf i'm doing {e.GetArg("???")}");
               });
                make.CreateCommand("voicech") //~make voicech
                .Alias(new string[] { "voicech" })
                .Description("Creates a new Voice Channel.") // not work yet
                .Parameter("????", ParameterType.Required)
                .Do(async e =>
               {
                   await e.Channel.SendMessage($"{e.User.Name} change it later {e.GetArg("???")}");
               });
            });
            //exemple//we would run our commands with ~do greet X and ~do bye X
            _client.GetService<CommandService>().CreateGroup("do", cgb =>
            {
                cgb.CreateCommand("greet")
                        .Alias(new string[] { "gr", "hi" })
                        .Description("Greets a person.")
                        .Parameter("GreetedPerson", ParameterType.Required)
                        .Do(async e =>
                        {
                            await e.Channel.SendMessage($"{e.User.Name} greets {e.GetArg("GreetedPerson")}");
                        });
                cgb.CreateCommand("bye")
                        .Alias(new string[] { "bb", "gb" })
                        .Description("Greets a person.")
                        .Parameter("GreetedPerson", ParameterType.Required)
                        .Do(async e =>
                        {
                            await e.Channel.SendMessage($"{e.User.Name} says goodbye to {e.GetArg("GreetedPerson")}");
                        });
            });
        }
    }
}

It works, the bot gets online. But It is an Echo Bot, and it just repeats every thing anyone say in the chat. How i fix this?

Also the commands didn’t work. What am i missing?

Edit :Made some changes, now it is no more an Echo Bot. I Think it now works as it should, i just need to fix and create more commands.
Still, i ask if someone can read it and look for any mistakes i could be made. Or show me command examples. I’ll keep it update as i add more cmds.

Thanks for helping me!

New code is on topic head!

  • Liked by
Reply
Cancel