public async Task RunBotAsync() { // first, let's load our configuration file var json = ""; using (var fs = File.OpenRead("config.json")) using (var sr = new StreamReader(fs, new UTF8Encoding(false))) json = await sr.ReadToEndAsync(); // next, let's load the values from that file // to our client's configuration var cfgjson = JsonConvert.DeserializeObject <ConfigJson>(json); var cfg = new DiscordConfiguration { Token = cfgjson.Token, TokenType = TokenType.Bot, AutoReconnect = true, MinimumLogLevel = LogLevel.Debug, }; // then we want to instantiate our client this.Client = new DiscordClient(cfg); // next, let's hook some events, so we know // what's going on this.Client.Ready += this.Client_Ready; this.Client.GuildAvailable += this.Client_GuildAvailable; this.Client.ClientErrored += this.Client_ClientError; // up next, let's set up our commands var ccfg = new CommandsNextConfiguration { // let's use the string prefix defined in config.json StringPrefixes = new[] { cfgjson.CommandPrefix }, // enable responding in direct messages EnableDms = true, // enable mentioning the bot as a command prefix EnableMentionPrefix = true }; // and hook them up this.Commands = this.Client.UseCommandsNext(ccfg); // let's hook some command events, so we know what's // going on this.Commands.CommandExecuted += this.Commands_CommandExecuted; this.Commands.CommandErrored += this.Commands_CommandErrored; // let's add a converter for a custom type and a name var mathopcvt = new MathOperationConverter(); Commands.RegisterConverter(mathopcvt); Commands.RegisterUserFriendlyTypeName <MathOperation>("operation"); // up next, let's register our commands this.Commands.RegisterCommands <ExampleUngrouppedCommands>(); this.Commands.RegisterCommands <ExampleGrouppedCommands>(); this.Commands.RegisterCommands <ExampleExecutableGroup>(); // set up our custom help formatter this.Commands.SetHelpFormatter <SimpleHelpFormatter>(); // finally, let's connect and log in await this.Client.ConnectAsync(); // when the bot is running, try doing <prefix>help // to see the list of registered commands, and // <prefix>help <command> to see help about specific // command. // and this is to prevent premature quitting await Task.Delay(-1); }
public async Task RunBotAsync() { // first, let's load our configuration file var json = ""; using (var fs = File.OpenRead("config.json")) using (var sr = new StreamReader(fs, new UTF8Encoding(false))) json = await sr.ReadToEndAsync(); // next, let's load the values from that file // to our client's configuration var cfgjson = JsonConvert.DeserializeObject <ConfigJson>(json); var cfg = new DiscordConfiguration { Token = cfgjson.Token, TokenType = TokenType.Bot, AutoReconnect = true, LogLevel = LogLevel.Debug, UseInternalLogHandler = true }; // then we want to instantiate our client this.Client = new DiscordClient(cfg); // If you are on Windows 7 and using .NETFX, install // DSharpPlus.WebSocket.WebSocket4Net from NuGet, // add appropriate usings, and uncomment the following // line //this.Client.SetWebSocketClient<WebSocket4NetClient>(); // If you are on Windows 7 and using .NET Core, install // DSharpPlus.WebSocket.WebSocket4NetCore from NuGet, // add appropriate usings, and uncomment the following // line //this.Client.SetWebSocketClient<WebSocket4NetCoreClient>(); // If you are using Mono, install // DSharpPlus.WebSocket.WebSocketSharp from NuGet, // add appropriate usings, and uncomment the following // line //this.Client.SetWebSocketClient<WebSocketSharpClient>(); // if using any alternate socket client implementations, // remember to add the following to the top of this file: //using DSharpPlus.Net.WebSocket; // next, let's hook some events, so we know // what's going on this.Client.Ready += this.Client_Ready; this.Client.GuildAvailable += this.Client_GuildAvailable; this.Client.ClientErrored += this.Client_ClientError; // up next, let's set up our commands var ccfg = new CommandsNextConfiguration { // let's use the string prefix defined in config.json StringPrefix = cfgjson.CommandPrefix, // enable responding in direct messages EnableDms = true, // enable mentioning the bot as a command prefix EnableMentionPrefix = true }; // and hook them up this.Commands = this.Client.UseCommandsNext(ccfg); // let's hook some command events, so we know what's // going on this.Commands.CommandExecuted += this.Commands_CommandExecuted; this.Commands.CommandErrored += this.Commands_CommandErrored; // let's add a converter for a custom type and a name var mathopcvt = new MathOperationConverter(); CommandsNextUtilities.RegisterConverter(mathopcvt); CommandsNextUtilities.RegisterUserFriendlyTypeName <MathOperation>("operation"); // up next, let's register our commands this.Commands.RegisterCommands <ExampleUngrouppedCommands>(); this.Commands.RegisterCommands <ExampleGrouppedCommands>(); this.Commands.RegisterCommands <ExampleExecutableGroup>(); // set up our custom help formatter this.Commands.SetHelpFormatter <SimpleHelpFormatter>(); // finally, let's connect and log in await this.Client.ConnectAsync(); // when the bot is running, try doing <prefix>help // to see the list of registered commands, and // <prefix>help <command> to see help about specific // command. // and this is to prevent premature quitting await Task.Delay(-1); }