Galsol.DiscordBot
1.1.0
See the version list below for details.
dotnet add package Galsol.DiscordBot --version 1.1.0
NuGet\Install-Package Galsol.DiscordBot -Version 1.1.0
<PackageReference Include="Galsol.DiscordBot" Version="1.1.0" />
paket add Galsol.DiscordBot --version 1.1.0
#r "nuget: Galsol.DiscordBot, 1.1.0"
// Install Galsol.DiscordBot as a Cake Addin #addin nuget:?package=Galsol.DiscordBot&version=1.1.0 // Install Galsol.DiscordBot as a Cake Tool #tool nuget:?package=Galsol.DiscordBot&version=1.1.0
Discord Bot
By Galsol
2018
If you have some knowledge in the topic, you may scroll down for shortened, non-detailed version on how to use.
This easy to use library offers fast, yet powerful way to create discord bots.
1. Creating the bot
First step is creating the bot account, To do so visit: https://discordapp.com/developers/applications/ Create new application, give it the best name you could find, a picture that will be remembered for ages and feed some cookies. In the 'Bot' tab (left) click "Add bot". We basically got a working bot account now. Hooray! \o/
2. Linking the bot to your server (or anyone else)
From 'General Information' copy the client id and copy it. Replace the "[Client_ID HERE]" from the link below. https://discordapp.com/api/oauth2/authorize?client_id=[Client_id HERE]&scope=bot&permissions=67584 Go to the link, enter your credentials and choose the server you wish to add the bot to.
3. Setting up the bot in C#
To set up the bot, in your Main function write:
Bot bot = new Bot("fileName.json", "botToken");
Note that you may replace fileName with a file name of your choice, for example channelID. Replace botToken with your bot token (Achieved from the 'Bot' tab in the developer portal), and run your program.
This should create a new JSON file in your run directory (default: project folder → bin → Debug/Release). Edit this JSON with your text editor of choice, insert between the 2 empty " the id of the room you want your bot to operate in. Easiest way to obtain the ID is open Discord, go to 'User Settings' → 'Appearance' and enable 'Developer Mode'. You can then simply right click the chat room and click 'Copy ID'. The end-result JSON file should look something like this:
{
"channel_id": "231239812389123912"
}
4. Programming your bot
Now you can start programming your bot. The way this works is: after creating your bot, write
bot.MessageCreated += __Your Function__
In Visual Studio it is enough to write bot.MessageCreated += and then hit [TAB]. Visual Studio auto-complete will auto-do its magic!
Examples:
bot.MessageCreated += MyFunc;
}
static void MyFunc(object sender, MessageEventArgs e)
{
...
}
bot.MessageCreated += (object sender, MessageEventArgs e) => {
...
};
}
5. Connecting your bot
Next (and final of the setup) is connecting your bot to the online world! Simple as that, the only thing you need to do is
bot.Connect(bot.state.[], [game], bot.status.[]);
The state and game are what will be displayed below the name of the bot. For example:
bot.Connect(bot.state.Playing, "C#", bot.status.Online);
Will appear on discord like:
Short Version
Step 1 .Create your bot at Developer Portal
Step 2. Authorize it and choose your server https://discordapp.com/api/oauth2/authorize?client_id={Add client ID here}&scope=bot&permissions=67584 (Make sure to add the client id of the bot in the url)
Step 3 .Create a JSON file in the same folder of your executable with a "channel_id" as key and as value a string that contains the ID of the room that you want your bot to operate in.
In Main() create a new Bot class:
Bot bot = new Bot("jsonFileName", "YourBotToken");
Add event handler to bot.MessageCreated
:
bot.MessageCreated += *yourFunction*;
Make your bot online!
bot.Connect(bot.state, game, bot.status);
Congratulations! Your bot is now operating!
Now to the cool stuff... Programming it.
Below is the documentation of how to program it.
Dont forget to using DiscordBot
at the start of the file!
If it doesn't find DiscordBot, right click on references in solution explorer → add a reference and enable DiscordBot.dll
Function and objects documentation
DiscordBot class
bot.postMessage(string content)
- Posts message in the room
MessageEventArgs
- contains Message object as msg
Message
- Includes every information about the message, i.e content, Author, timestamp etc.
Author
- Contains information about message author. i.e username, id.
Files class
save(fileName, Dictionary)
- Saves the dictionary of <string, string> or <string, int> to the fileName.json
read(fileName)
- Reads and returns a dictionary<string, string> from fileName.json
readI(fileName)
- Reads and returns a dictionary<string, int> from fileName.json
All json files are stored at executablePath/Data/
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net is compatible. |
-
- Newtonsoft.Json (>= 11.0.0)
- System.Windows.Forms (>= 4.0.0)
- websocket-sharp (>= 1.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.