Advertisement
Guest User

Botcode

a guest
Apr 19th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. ((First tab))
  2. using Discord;
  3. using Discord.WebSocket;
  4. using System.Threading.Tasks;
  5.  
  6.  
  7. namespace IMP
  8. {
  9. public class Program
  10. {
  11. static void Main(string[] args)
  12. => new Program().StartAsync().GetAwaiter().GetResult();
  13.  
  14. private DiscordSocketClient _client;
  15.  
  16. private CommandHandler _handler;
  17.  
  18. public async Task StartAsync()
  19. {
  20. _client = new DiscordSocketClient();
  21.  
  22. new CommandHandler(_client);
  23.  
  24. await _client.LoginAsync(TokenType.Bot, "xxxxxxxxxxxx");
  25.  
  26. await _client.StartAsync();
  27.  
  28. _handler = new CommandHandler(_client);
  29.  
  30. await Task.Delay(-1);
  31. }
  32. }
  33. }
  34.  
  35.  
  36.  
  37.  
  38. ((Second tab))
  39.  
  40. using Discord.Commands;
  41. using Discord.WebSocket;
  42. using System.Reflection;
  43. using System.Threading.Tasks;
  44.  
  45. namespace IMP
  46. {
  47. public class CommandHandler
  48. {
  49. private DiscordSocketClient _client;
  50.  
  51. private CommandService _service;
  52.  
  53. public CommandHandler(DiscordSocketClient client)
  54. {
  55. _client = client;
  56.  
  57. _service = new CommandService();
  58.  
  59. _service.AddModulesAsync(Assembly.GetEntryAssembly());
  60.  
  61. _client.MessageReceived += HandleCommandAsync;
  62.  
  63. }
  64.  
  65. private async Task HandleCommandAsync(SocketMessage s)
  66. {
  67. var msg = s as SocketUserMessage;
  68. if (msg == null) return;
  69.  
  70. var context = new SocketCommandContext(_client, msg);
  71.  
  72. int argPos = 0;
  73. if (msg.HasCharPrefix('/', ref argPos))
  74. {
  75. var result = await _service.ExecuteAsync(context, argPos);
  76.  
  77. if (!result.IsSuccess && result.Error != CommandError.UnknownCommand)
  78. {
  79. await context.Channel.SendMessageAsync(result.ErrorReason);
  80. }
  81. }
  82. }
  83. }
  84. }
  85.  
  86.  
  87.  
  88. ((Third tab))
  89.  
  90. using Discord;
  91. using Discord.Commands;
  92. using System.Threading.Tasks;
  93.  
  94. namespace IMP.Modules
  95. {
  96. public class Test : ModuleBase<SocketCommandContext>
  97. {
  98. [Command("UserID")]
  99. public async Task Bastard(IGuildUser user)
  100. {
  101. await Context.Channel.SendMessageAsync(user.ToString());
  102. }
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement