Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ciel/5a706e823c82b92c678d481edaf38761 to your computer and use it in GitHub Desktop.

Select an option

Save ciel/5a706e823c82b92c678d481edaf38761 to your computer and use it in GitHub Desktop.
Basic discord bot with multiple commands C#
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