Guest User

Untitled

a guest
Jun 20th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. using Discord.Commands;
  2. using Discord.WebSocket;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Reflection;
  9. using DiscordTutorialBot.Core.LevelingSystem;
  10. using DiscordTutorialBot.Core.UserAccounts;
  11.  
  12. namespace DiscordTutorialBot
  13. {
  14. class CommandHandler
  15. {
  16. DiscordSocketClient _client;
  17. CommandService _service;
  18.  
  19. public async Task InitializeAsync(DiscordSocketClient client)
  20. {
  21. _client = client;
  22. _service = new CommandService();
  23. await _service.AddModulesAsync(Assembly.GetEntryAssembly());
  24. _client.MessageReceived += HandleCommandAsync;
  25. }
  26.  
  27. private async Task HandleCommandAsync(SocketMessage s)
  28. {
  29. if (!(s is SocketUserMessage msg)) return;
  30. var context = new SocketCommandContext(_client, msg);
  31. if (context.User.IsBot) return;
  32.  
  33. // Mute check
  34. var userAccount = UserAccounts.GetAccount(context.User);
  35.  
  36. if (CheckIfMuted(context.User))
  37. {
  38. await context.Message.DeleteAsync();
  39. return;
  40. }
  41.  
  42. // Leveling up
  43. Leveling.UserSentMessage((SocketGuildUser)context.User, (SocketTextChannel)context.Channel);
  44.  
  45. int argPos = 0;
  46. if (msg.HasStringPrefix(Config.bot.cmdPrefix, ref argPos)
  47. || msg.HasMentionPrefix(_client.CurrentUser, ref argPos))
  48. {
  49. var result = await _service.ExecuteAsync(context, argPos);
  50. if (!result.IsSuccess && result.Error != CommandError.UnknownCommand)
  51. {
  52. Console.WriteLine(result.ErrorReason);
  53. }
  54. }
  55. // warn
  56.  
  57.  
  58. }
  59. private bool CheckIfMuted(SocketUser contextUser)
  60. {
  61. UserAccount account = UserAccounts.GetAccount(contextUser);
  62. return account.UnmuteTime - DateTime.Now <= TimeSpan.Zero;
  63. }
  64. }
  65. }
Add Comment
Please, Sign In to add comment