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); }
private async Task MainAsync() { // getting versions and assembly info SetBotVersionInfo(); // init log channels _logChannels = new List <DiscordChannel>(); _lastLogChWrite = File.GetLastWriteTime("logchannels.txt"); string json = await FileHandler.ReadJsonConfig(); if (json.Length == 0) { return; } if (json == "default") { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine("Created default config file.\n" + "Now you need to get your discord bot token " + "and put it in config.json file.\n" + "Also make sure you set other parameters."); Console.ResetColor(); return; } // setting up client var cfgjson = JsonSerializer.Deserialize <ConfigJson>(json); if (cfgjson == null) { return; } var cfg = new DiscordConfiguration { Token = cfgjson.Token, TokenType = TokenType.Bot, AutoReconnect = true, MinimumLogLevel = RuntimeInfo == "Debug" ? LogLevel.Debug : LogLevel.Information, MessageCacheSize = 2048, LogTimestampFormat = "dd-MM-yyyy HH:mm:ss zzz" }; // client init and event hooks _discord = new DiscordClient(cfg); _discord.Ready += Discord_Ready; _discord.GuildAvailable += Discord_GuildAvailable; _discord.GuildUnavailable += Discord_GuildUnavailable; _discord.GuildCreated += Discord_GuildCreated; _discord.GuildDeleted += Discord_GuildDeleted; _discord.ChannelDeleted += Discord_ChannelDeleted; _discord.DmChannelDeleted += Discord_DmChannelDeleted; _discord.GuildDownloadCompleted += Discord_GuildDownloadCompleted; _discord.ClientErrored += Discord_ClientErrored; _discord.SocketClosed += Discord_SocketClosed; _discord.Resumed += Discord_Resumed; _discord.Heartbeated += Discord_Heartbeated; // setting up interactivity var intcfg = new InteractivityConfiguration { Timeout = TimeSpan.FromMinutes(cfgjson.ActTimeout), PaginationDeletion = PaginationDeletion.DeleteMessage, PollBehaviour = PollBehaviour.KeepEmojis }; _discord.UseInteractivity(intcfg); // setting up commands var cmdcfg = new CommandsNextConfiguration { StringPrefixes = new List <string> { cfgjson.CommandPrefix }, EnableDms = cfgjson.DmsEnabled, EnableMentionPrefix = cfgjson.MentionEnabled, CaseSensitive = cfgjson.CaseSensitive, EnableDefaultHelp = true }; // commands hooks and register _commands = _discord.UseCommandsNext(cmdcfg); _commands.CommandExecuted += Commands_CommandExecuted; _commands.CommandErrored += Commands_CommandErrored; _commands.RegisterCommands <Commands.Commands>(); _commands.RegisterCommands <LeetCommands>(); _commands.RegisterCommands <Interactivities.Interactivities>(); _commands.RegisterCommands <Administrative>(); _commands.RegisterCommands <Cats>(); _commands.RegisterCommands <DiceRolling>(); _commands.RegisterCommands <CryptoAes>(); _commands.RegisterCommands <CryptoRsa>(); _commands.RegisterCommands <MathCommands>(); _commands.RegisterCommands <StatusCommands>(); _commands.RegisterCommands <VoiceCommands>(); // adding math converter for custom type and name var mathopscvrt = new MathOperationConverter(); _commands.RegisterConverter(mathopscvrt); _commands.RegisterUserFriendlyTypeName <MathOperation>("operator"); // setting up and enabling voice var vcfg = new VoiceNextConfiguration { AudioFormat = AudioFormat.Default, EnableIncoming = false }; _discord.UseVoiceNext(vcfg); // setting custom help formatter _commands.SetHelpFormatter <HelpFormatter>(); // init twitch live and youtube video monitors _ttvApIclid = cfgjson.TwitchApiClid; _ttvApIsecret = cfgjson.TwitchApiSecret; _ytApIkey = cfgjson.YoutubeApiKey; _tlm = new TwitchLiveMonitor(); _yvm = new YoutubeVideoMonitor(); // getting aes and rsa keys from config AesKey = cfgjson.AesKey; AesIv = cfgjson.AesIv; RsaPublicKey = cfgjson.RsaPublicKey; RsaPrivateKey = cfgjson.RsaPrivateKey; // connecting discord try { await _discord.ConnectAsync(); } catch (Exception e) { _discord.Logger.LogCritical($"{e.Message}"); return; } await Task.Delay(-1); }