Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Threading.Tasks;
- using System.Reflection;
- using Discord.Commands;
- using Discord.WebSocket;
- using Discord;
- namespace MyBot
- {
- public class CommandHandler
- {
- private CommandService commands;
- private DiscordSocketClient client;
- private IDependencyMap map;
- public async Task Install(IDependencyMap _map)
- {
- //Create Command Service, Inject it into Dependency Map
- client = _map.Get<DiscordSocketClient>();
- commands = new CommandService();
- _map.Add(commands);
- map = _map;
- await commands.AddModulesAsync(Assembly.GetEntryAssembly());
- //Send user message to get handled
- client.MessageReceived += HandleCommand;
- client.UserJoined += AnnounceJoinUser;
- client.UserLeft += AnnounceLeftUser;
- }
- public async Task AnnounceLeftUser(SocketGuildUser user)
- {
- var channel = client.GetChannel() as SocketTextChannel;
- await channel.SendMessageAsync("");
- }
- public async Task AnnounceJoinUser(SocketGuildUser user)
- {
- var channel = client.GetChannel() as SocketTextChannel;
- await channel.SendMessageAsync("");
- }
- public async Task HandleCommand(SocketMessage parameterMessage)
- {
- //Don't handle the command if it is a system message
- var message = parameterMessage as SocketUserMessage;
- if (message == null) return;
- //Mark where the prefix ends and the command begins
- int argPos = 0;
- //Determine if the message has a valid prefix, adjust argPos
- if (!(message.HasMentionPrefix(client.CurrentUser, ref argPos) || message.HasCharPrefix('!', ref argPos))) return;
- //Create a Command Context
- var context = new CommandContext(client, message);
- //Execute the command, store the result
- var result = await commands.ExecuteAsync(context, argPos, map);
- //If the command failed, notify the user
- if (!result.IsSuccess)
- await message.Channel.SendMessageAsync($"**Error:** {result.ErrorReason}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement