Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.15 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 mayPAWBot
  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.  
  19.         public async Task RunBotAsync()
  20.         {
  21.             _client = new DiscordSocketClient();
  22.             _commands = new CommandService();
  23.  
  24.             _services = new ServiceCollection()
  25.                 .AddSingleton(_client)
  26.                 .AddSingleton(_commands)
  27.                 .BuildServiceProvider();
  28.  
  29.             string botToken = "NTIyNjIyNDI4MzcxNzQ2ODI2.DvON7Q.vAwKDVMctbOf5mMPu-6-F7FCvqg";
  30.  
  31.             //event subscriptions
  32.             _client.Log += Log;
  33.  
  34.             await RegisterCommandsAsync();
  35.  
  36.             await _client.LoginAsync(TokenType.Bot, botToken);
  37.  
  38.             await _client.StartAsync();
  39.  
  40.             await Task.Delay(-1);
  41.         }
  42.  
  43.         private Task Log(LogMessage arg)
  44.         {
  45.             Console.WriteLine(arg);
  46.  
  47.             return Task.CompletedTask;
  48.         }
  49.  
  50.         public async Task RegisterCommandsAsync()
  51.         {
  52.             _client.MessageReceived += HandleCommandAsync;
  53.  
  54.             await _commands.AddModulesAsync(Assembly.GetEntryAssembly());
  55.         }
  56.  
  57.         private async Task HandleCommandAsync(SocketMessage arg)
  58.         {
  59.             var message = arg as SocketUserMessage;
  60.  
  61.             if (message is null || message.Author.IsBot) return;
  62.  
  63.             int argPos = 0;
  64.  
  65.             if (message.HasStringPrefix("!", ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos))
  66.             {
  67.                 var context = new SocketCommandContext(_client, message);
  68.  
  69.                 var result = await _commands.ExecuteAsync(context, argPos, _services);
  70.  
  71.                 if (!result.IsSuccess)
  72.                     Console.WriteLine(result.ErrorReason);
  73.             }
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement