Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. using Discord;
  2. using Discord.Commands;
  3. using Discord.WebSocket;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using System;
  6. using System.Reflection;
  7. using System.Threading.Tasks;
  8.  
  9. namespace polar
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args) => new Program().RunBotAsync().GetAwaiter().GetResult();
  14.  
  15. private DiscordSocketClient _client;
  16. private CommandService _commands;
  17. private IServiceProvider _services;
  18. public async Task RunBotAsync()
  19. {
  20. _client = new DiscordSocketClient();
  21. _commands = new CommandService();
  22.  
  23. _services = new ServiceCollection()
  24. .AddSingleton(_client)
  25. .AddSingleton(_services)
  26. .BuildServiceProvider();
  27. string botToken = "lZFJ3yVrGUZX95aVmlXgb5yYpQ_SoTKP";
  28.  
  29. // event subscription
  30. _client.Log += Log;
  31.  
  32. await RegisterCommandsAsync();
  33.  
  34. await _client.LoginAsync(TokenType.Bot, botToken);
  35.  
  36. await _client.StartAsync();
  37.  
  38. await Task.Delay(-1);
  39. }
  40.  
  41. private Task Log(LogMessage arg)
  42. {
  43. Console.WriteLine(arg);
  44. return Task.CompletedTask;
  45. }
  46.  
  47. public async Task RegisterCommandsAsync()
  48. {
  49. _client.MessageReceived += HandleCOmmaandAsunc;
  50. await _commands.AddModulesAsync(Assembly.GetEntryAssembly());
  51.  
  52. }
  53.  
  54. private async Task HandleCOmmaandAsunc(SocketMessage arg)
  55. {
  56. var message = arg as SocketUserMessage;
  57.  
  58. if (message is null || message.Author.IsBot) return;
  59.  
  60. int argPos = 0;
  61.  
  62. if (message.HasStringPrefix("$", ref argPos)|| message.HasMentionPrefix(_client.CurrentUser, ref argPos))
  63. {
  64. var context = new SocketCommandContext(_client, message);
  65.  
  66. var result = await _commands.ExecuteAsync(context, argPos, _services);
  67.  
  68. if (!result.IsSuccess)
  69. Console.WriteLine(result.ErrorReason);
  70.  
  71.  
  72. }
  73.  
  74. }
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement