/// <summary> /// Method to start the bot. /// </summary> public void Start() { log.Info("Application is starting"); Initializer config = new Initializer().run(); Queue <Command> audioQueue = new Queue <Command>(); _client.Log.Message += (s, e) => // Log Discord.Net messages to log4net { if (e.Severity == LogSeverity.Info) { log.Info(e.Message); } else if (e.Severity == LogSeverity.Warning) { log.Warn(e.Message); } else if (e.Severity == LogSeverity.Error) { log.Error(e.Message); } }; _client.UsingAudio(x => // Opens an AudioConfigBuilder so we can configure our AudioService { x.Mode = AudioMode.Outgoing; // Tells the AudioService that we will only be sending audio }); //var _vService = _client.GetService<AudioService>(); _client.UsingCommands(x => { x.PrefixChar = config.CommandChar; x.HelpMode = HelpMode.Public; }); createCommands(config, _client, audioQueue); createFileSystemWatcher(config); _client.ExecuteAndWait(async() => { log.Info("Connecting to Discord..."); int attempts = 0; while (attempts < 3) { try { await _client.Connect(config.TOKEN); break; } catch (Discord.Net.HttpException e) { log.Error(string.Format("{0}: {1}", e.GetType().ToString(), e.Message)); attempts++; } catch (System.Net.WebException e) { log.Error(string.Format("{0}: {1}", e.GetType().ToString(), e.Message)); attempts++; } catch (Exception e) { log.Error(string.Format("{0}: {1}", e.GetType().ToString(), e.Message)); attempts++; } } if (attempts == 3) //Failed to connect 3 times; check for errors in config file { log.Warn("Failed to connect. Exiting"); Environment.Exit(1); } }); }
/// <summary> /// Create the commands from the commands in the config file for Discord.Net /// </summary> private void createCommands(Initializer config, DiscordClient _client, Queue <Command> audioQueue) { foreach (Audio command in config.commands) { CommandBuilder cb = _client.GetService <CommandService>().CreateCommand(command.Command); if (command.Alias != null) { cb.Alias(command.Alias); } if (command.Description != null) { cb.Description(command.Description); } cb.Parameter("AudioIndex", ParameterType.Optional); cb.Do(e => { if (e.User.VoiceChannel != null) { log.Info(command.Paths); if (command.Paths.Length == 1) { addAudioToQueue(command.Paths[0], e.User.VoiceChannel, audioQueue); } else //Multiple options for audio { int parseValue, index; int.TryParse(e.GetArg("AudioIndex"), out parseValue); if (parseValue != 0 && parseValue <= command.Paths.Length) { index = parseValue; } else { Random r = new Random(); index = r.Next(command.Paths.Length); } addAudioToQueue(command.Paths[index - 1], e.User.VoiceChannel, audioQueue); } if (!audioPlaying) { sendAudioQueue(audioQueue); } log.Info(string.Format("Received command: {1} from: {0} in {2} on {3}", e.User, command.Command, e.Channel, e.Server)); } }); } _client.GetService <CommandService>().CreateCommand("ayy") .Description("Test if the bot is receiving messages") .Do(e => { log.Info(string.Format("Ayy received from: {0} in {1} on {2}", e.User, e.Channel, e.Server)); try { e.Channel.SendMessage("lmao"); log.Info("Sent lmao"); } catch (Discord.Net.TimeoutException er) { log.Error(string.Format("{0}: {1}", er.GetType().ToString(), er.Message)); } }); }