Forked from Filipemnzs/gist:a4593156188ca4b5e3811ddc3306335f
Created
September 22, 2018 12:35
-
-
Save ciel/4aeaf0a4bd4ad590f1859c10f0b37344 to your computer and use it in GitHub Desktop.
Basic discord bot with multiple commands C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Discord; | |
| using Discord.Commands; | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| namespace FlippyBot | |
| { | |
| class MyBot | |
| { | |
| CommandService commands; | |
| DiscordClient discord; | |
| Random rand; | |
| string[] Memes; | |
| string[] EightBall; | |
| public MyBot() | |
| { | |
| rand = new Random(); | |
| Memes = new string[] | |
| { | |
| "meme/blob.png", | |
| "meme/hehexd.jpg", | |
| "meme/Maddie&Jorge.png", | |
| "meme/Maddie.png", | |
| "meme/spooky.gif", | |
| "meme/Stinky.png", | |
| "meme/thicc.png", | |
| "meme/hehexd.jpg" | |
| }; | |
| EightBall = new string[] | |
| { | |
| "It is certain.", | |
| "Without a doubt.", | |
| "Yes, definetly.", | |
| "Yes.", | |
| "Reply hazy try again.", | |
| "?", | |
| "Bitch, I might be.", | |
| "idk", | |
| "No.", | |
| "Fuck no.", | |
| "Very doubtful.", | |
| "My sources say no." | |
| }; | |
| discord = new DiscordClient(x => | |
| { | |
| x.LogLevel = LogSeverity.Info; | |
| x.LogHandler = log; | |
| }); | |
| discord.UsingCommands(x => | |
| { | |
| x.PrefixChar = '$'; | |
| x.AllowMentionPrefix = true; | |
| }); | |
| commands = discord.GetService<CommandService>(); | |
| //calls commands | |
| Register8BallCommand(); | |
| RegisterMemeCommand(); | |
| RegisterPurgeCommand(); | |
| //Hello world command | |
| commands.CreateCommand("hello") | |
| .Do(async (e) => | |
| { | |
| await e.Channel.SendMessage("Hello world!"); | |
| }); | |
| //discord bot token | |
| discord.ExecuteAndWait(async () => | |
| { | |
| await discord.Connect("<your bot token>", TokenType.Bot); | |
| }); | |
| } | |
| //logs that the bot connected to the server | |
| private void log (object sender, LogMessageEventArgs e) | |
| { | |
| Console.WriteLine(e.Message); | |
| } | |
| //sends random picture from array | |
| private void RegisterMemeCommand() | |
| { | |
| commands.CreateCommand("xd") | |
| .Do(async (e) => | |
| { | |
| int randomMemeIndex = rand.Next(Memes.Length); | |
| string memeToPost = Memes[randomMemeIndex]; | |
| await e.Channel.SendFile(memeToPost); | |
| }); | |
| } | |
| //sends random message from array | |
| private void Register8BallCommand() | |
| { | |
| commands.CreateCommand("8Ball") | |
| .Do(async (e) => | |
| { | |
| int randomEightBallMessage = rand.Next(EightBall.Length); | |
| string messageToPost = EightBall[randomEightBallMessage]; | |
| await e.Channel.SendMessage(messageToPost); | |
| }); | |
| } | |
| //deletes last 100 messages in the channel | |
| private void RegisterPurgeCommand() | |
| { | |
| commands.CreateCommand("purge") | |
| .Do(async (e) => | |
| { | |
| Message[] messagesToDelete; | |
| messagesToDelete = await e.Channel.DownloadMessages(100); | |
| await e.Channel.DeleteMessages(messagesToDelete); | |
| }); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment