Advertisement
jreppa

Tarkov Novelty Program

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