private static void OnCommandException(CommandExceptionEventArgs e) { Exception ex = e.Exception; CommandEventArgs command = e.Command; DateTime now = DateTime.Now; string nowString = "[" + now.ToShortTimeString() + "]"; StringBuilder messageBuilder = new StringBuilder(); messageBuilder.Append( nowString + "An error occured in the command \"" + command.Message.RawText + "\" by " + command.User.Name + ": "); messageBuilder.Append(e.Exception.ToString()); messageBuilder.Append("In channel " + command.Channel.Name); messageBuilder.Append(""); BotDebug.Log(messageBuilder.ToString(), "Error", ConsoleColor.Red); command.Channel.SendMessage("```Une erreur s'est produite à l'exécution de la commande.```"); }
public static void ReadRoastStats() { List <int> linesRead = new List <int>(); try { using (StreamReader reader = new StreamReader(roastStatsFileName)){ while (!reader.EndOfStream) { int value; if (int.TryParse(reader.ReadLine().Trim(), out value)) { linesRead.Add(value); } else { linesRead.Add(0); } } } } catch (FileNotFoundException) { BotDebug.Log("Le fichier \"" + roastStatsFileName + "\" n'a pas été trouvé", "FileError", ConsoleColor.Red); } roastStats = linesRead.ToArray(); }
private static void SaveLine(string filename, string line) { StreamWriter writer; try { using (writer = new StreamWriter(filename, true)) { writer.WriteLine(line); } } catch (FileNotFoundException) { BotDebug.Log("Le fichier \"" + filename + "\" n'a pas été trouvé", "FileError", ConsoleColor.Red); } }
static async Task Roast(CommandEventArgs args) { if (args.Server.Id == 210360089318522880ul) { await args.Channel.SendMessage("Commande désactivée dans ce sereur pour en respecter le climat pédagogique ¯\\_(ツ)_/¯"); return; } string target = args.GetArg("cible"); string targetName = "qqn"; if (target == null) { if (args.Channel.IsPrivate) { targetName = args.User.Name; } else { User[] users = args.Server.Users.Where((u) => (u.Status != UserStatus.Offline)).ToArray(); targetName = Bot.getUserName(users[Bot.rand.Next(users.Length)]); } } else if (target.ToLower() == "me") { targetName = args.User.Name; } else { ulong id = 0; if (Bot.TryGetIdFromMention(target, ref id)) { targetName = Bot.getUserName(args.Server.GetUser(id)); } else { targetName = target; } } int chosenRoast = Bot.rand.Next(roastsList.Length); string roast = String.Format(roastsList[chosenRoast], targetName); BotDebug.OnRoast(chosenRoast); await args.Channel.SendMessage(roast); }
static void Main(string[] args) { Console.Title = "Funbot"; Bot bot = Bot.botInstance; ImageGenerator img = new ImageGenerator(); BotDebug.InitDebug(); BotDebug.GamesChanged += BotDebug_GamesChangedEvent; BotDebug.RoastsChanged += BotDebug_RoastsChangedEvent; BotDebug.Log("Ajout des commandes", "Init"); bot.commandService.AddCommands(typeof(BotDebug), null); bot.commandService.AddCommands(typeof(Questions), null); bot.commandService.AddCommands(typeof(ImageGenerator), img); bot.commandService.AddCommands(typeof(Program), null); bot.commandService.AddCommands(typeof(ServerManagement), null); bot.DiscordClient.Ready += DiscorClient_Ready; bot.commandService.CommandException += OnCommandException; BotDebug.Log("Lecture des jeux", "Init"); gamesList = LoadLines(gameFileName); BotDebug.Log("Lecture des roasts"); roastsList = LoadLines(roastFileName); gameTimer.AutoReset = true; gameTimer.Elapsed += GameTimer_Elapsed; WriteLine("Connexion..."); Console.CursorTop--; bot.Connect(); gameTimer.Start(); Console.ReadKey(); bot.Disconnect(); BotDebug.Log("Fun Bot déconnecté"); BotDebug.StopDebug(); WriteLine("Fin du programme"); }
static async Task AddGame(CommandEventArgs args) { string gamename = args.GetArg("gamename"); if (!gamesList.Contains(gamename)) { string[] newGameList = new string[gamesList.Length + 1]; gamesList.CopyTo(newGameList, 0); newGameList[gamesList.Length] = gamename; gamesList = newGameList; SaveLine(gameFileName, gamename); await args.Channel.SendMessage("Le jeu " + gamename + " à été ajouté."); BotDebug.Log("A game has been added (" + gamename + ")"); } }
public static void SaveRoastStats() { StreamWriter writer; try { using (writer = new StreamWriter(roastStatsFileName, false)) { foreach (int count in roastStats) { writer.WriteLine(count.ToString()); } } } catch (FileNotFoundException) { BotDebug.Log("Le fichier \"" + roastStatsFileName + "\" n'a pas été trouvé", "FileError", ConsoleColor.Red); } }
private static void SaveLines(string filename, string[] lines) { StreamWriter writer; try { using (writer = new StreamWriter(filename, false)) { foreach (string name in lines) { if (name != "") { writer.WriteLine(name); } } } } catch (FileNotFoundException) { BotDebug.Log("Le fichier \"" + filename + "\" n'a pas été trouvé", "FileError", ConsoleColor.Red); } }
private static string[] LoadLines(string filename) { List <string> linesRead = new List <string>(); try { StreamReader reader = new StreamReader(filename); while (!reader.EndOfStream) { linesRead.Add(reader.ReadLine()); } reader.Close(); } catch (FileNotFoundException) { BotDebug.Log("Le fichier \"" + filename + "\" n'a pas été trouvé", "FileError", ConsoleColor.Red); } return(linesRead.ToArray()); }
static async Task AddRoast(CommandEventArgs args) { if (args.Server.Id == 210360089318522880ul) { await args.Channel.SendMessage("Commande désactivée dans ce sereur pour en respecter le climat pédagogique ¯\\_(ツ)_/¯"); return; } string roast = args.GetArg("roast"); if (!gamesList.Contains(roast)) { string[] newRoastList = new string[roastsList.Length + 1]; roastsList.CopyTo(newRoastList, 0); newRoastList[roastsList.Length] = roast; roastsList = newRoastList; SaveLine(roastFileName, roast); await args.Channel.SendMessage(String.Format("Le roast " + roast + " a été ajouté", "(...)")); BotDebug.Log("A roast has been added (" + roast + ")"); } }
static async Task AddRoomCmd(CommandEventArgs args) { bool isSecret; string privacyArg = args.GetArg("privacy"); string roomName = args.GetArg("roomName"); if (privacyArg == null || privacyArg == "public") { isSecret = false; } else if (privacyArg == "secret") { isSecret = true; } else { await args.Channel.SendMessage("L'argument \"privacy\" doit être soit \"public\" ou \"secret\""); return; } if (roomName == null) { await args.Channel.SendMessage("Vous devez spécifier un nom."); } else { Role botRole = null; Role everyoneRole = args.Server.EveryoneRole; ChannelPermissionOverrides botPermission = new ChannelPermissionOverrides( readMessages: PermValue.Allow, sendMessages: PermValue.Allow, manageChannel: PermValue.Allow ); ChannelPermissionOverrides notAllowedPermission = new ChannelPermissionOverrides( readMessages: PermValue.Deny ); foreach (Role r in args.Server.Roles) { if (r.Name == "Bot") { botRole = r; break; } } if (botRole == null) { throw new Exception("Le rôle de Bot n'a pas été trouvé"); } Channel newChannel = await args.Server.CreateChannel(roomName, ChannelType.Text); await newChannel.AddPermissionsRule(botRole, botPermission); await newChannel.AddPermissionsRule(args.User, botPermission); BotDebug.Log("Le channel " + roomName + " à été créé par " + args.User.Name, "ServerManagement", ConsoleColor.Yellow); if (isSecret) { await newChannel.AddPermissionsRule(everyoneRole, notAllowedPermission); } await newChannel.SendMessage(args.User.Mention + ", votre nouveau salon à été créé, vous pouvez en modifier les permissions.\n" + "Prenez notes que les administrateurs ont toujours accès à ce salon."); } }
static void BotDebug_GamesChangedEvent() { BotDebug.Log("Lecture des nouveau jeux"); gamesList = LoadLines(gameFileName); }
static void BotDebug_RoastsChangedEvent() { BotDebug.Log("Lecture des nouveau roast roasts"); roastsList = LoadLines(roastFileName); }