/// <summary> /// Set the executing command of a user. /// </summary> /// <param name="id">The id of the user</param> /// <param name="cmd">The command to set as executing</param> public void SetExecutingCommand(ulong id, IDiscordCommand cmd) { if (ExecutingCommand.ContainsKey(id)) { ExecutingCommand[id] = cmd; } else { ExecutingCommand.Add(id, cmd); } }
/// <summary> /// Handles a Discord Message and manages dynamic dispatch. /// </summary> /// <param name="msg">The Discord text message</param> /// <param name="current_user">The id of the user that sent the message</param> /// <returns></returns> public async Task Handle(SocketMessage msg, ulong current_user) { // Check if the msg mentions the bot. // TODO: Add conditional replies if (msg.Content.Trim().StartsWith($"<@{current_user}>") || msg.Content.Trim().StartsWith($"<@!{current_user}>")) { // Send a message with the current server prefix and other help data. await msg.Channel.SendMessageAsync(null, false, new EmbedBuilder() .WithTitle("Clubby") .AddField("The server prefix is:", $"'{Program.config.DiscordBotPrefix}'\nRun `{Program.config.DiscordBotPrefix}help` for more info") .Build()); } // Check if the user that sent the message is executing a command else if (ExecutingCommand.ContainsKey(msg.Author.Id) && ExecutingCommand[msg.Author.Id] != null) { // If the command is a cancel call, set the currently executing command to null and inform the user if (msg.Content.StartsWith($"{Program.config.DiscordBotPrefix}cancel")) { await msg.Channel.SendMessageAsync($"{ExecutingCommand[msg.Author.Id].GetCommandHelp().CommandName} command execution has been cancelled!"); ExecutingCommand[msg.Author.Id] = null; } // Dispatch to the executing command else { try { await ExecutingCommand[msg.Author.Id].Handle(msg, (msg.Channel as SocketGuildChannel) != null ? (msg.Channel as SocketGuildChannel).Guild : null, this); } catch (Exception e) { // If the command fails notify the user as to why. await msg.Channel.SendError(e.Message); } } } // If the message is a command it will start with the current prefix else if (msg.Content.StartsWith(Program.config.DiscordBotPrefix)) { // Get the command name from the message string command = ExtractCommand(msg.Content); if (command == null) { var guess = command_names.GetBestMatch(msg.Content.Split(' ')[0].Substring(Program.config.DiscordBotPrefix.Length), 4); if (guess == null) { await msg.Channel.SendError($"Couldn't find command: `{Program.config.DiscordBotPrefix}{msg.Content.Split(' ')[0].Substring(Program.config.DiscordBotPrefix.Length)}`"); } else { await msg.Channel.SendError($"Couldn't find command: `{Program.config.DiscordBotPrefix}{msg.Content.Split(' ')[0].Substring(Program.config.DiscordBotPrefix.Length)}`\n\nDid you mean `{Program.config.DiscordBotPrefix}{guess.ToLower()}`"); } return; } // Get the type of the command and construct an instance to handle the command. IDiscordCommand cmd = commands.GetInstance(Construct_Identifier(command, (msg.Author as SocketGuildUser).ThenOrElse( user => user.Guild.Id, (ulong)0 ))); // If the command existed cmd will be non null if (cmd != null) { // Get the minimum permission required to run this command int perms = (int)cmd.GetMinimumPerms(); // Get the permission that the user holds int user_perms = Program.config.DiscordPermissions.GetResolvedPerms(msg.Author, msg.IsFromOwner()); // Check if the user can use the command if (user_perms >= perms) { // Sprinkle in a small chance of rebellion if (rand.NextDouble() > 0.999) { await msg.Channel.SendMessageAsync(null, false, new EmbedBuilder().WithTitle("No").WithDescription("no").WithColor(Color.Red).Build()); return; } // If the bot is cooperative execute the command's local handler. try { await cmd.Handle(msg, (msg.Channel as SocketGuildChannel) != null?(msg.Channel as SocketGuildChannel).Guild : null, this); // Update the number of times this command has been used. CommandExecutionIncrement(command); } catch (Exception e) { // If the command fails notify the user as to why. await msg.Channel.SendError(e.Message); File.WriteAllText("./last_error.txt", e.StackTrace); } } // Reject the execution attempt if the user doesn't have the expected permissions else { await msg.Channel.SendMessageAsync("You don't have permissions to use this command!"); } } else { // Couldn't find the command so let them know. await msg.Channel.SendError($"Couldn't find command: `{Program.config.DiscordBotPrefix}{command}`"); } } }
public bool CommandExist(string commandName, out IDiscordCommand cmd) { cmd = Commands.SingleOrDefault(c => c.Name.ToLower().Trim() == commandName); return(cmd != null); }