Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Discord;
- using Discord.Commands;
- using Discord.WebSocket;
- using Microsoft.Extensions.DependencyInjection;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- namespace DiscordBotTarkov
- {
- class Program
- {
- static void Main(string[] args) => new Program().RunBotAsync().GetAwaiter().GetResult();
- private DiscordSocketClient _client;
- private CommandService _commands;
- private IServiceProvider _services;
- public async Task RunBotAsync()
- {
- _client = new DiscordSocketClient();
- _commands = new CommandService();
- _services = new ServiceCollection()
- .AddSingleton(_client)
- .AddSingleton(_commands)
- .BuildServiceProvider();
- string botToken = "ENTER BOT TOKEN HERE";
- //event subsctiptions
- _client.Log += Log;
- await RegisterCommandsAsync();
- await _client.LoginAsync(Discord.TokenType.Bot, botToken);
- await _client.StartAsync();
- await Task.Delay(-1);
- }
- private Task Log(LogMessage arg)
- {
- Console.WriteLine(arg);
- return Task.CompletedTask;
- }
- public async Task RegisterCommandsAsync()
- {
- _client.MessageReceived += HandleCommandAsync;
- await _commands.AddModulesAsync(Assembly.GetEntryAssembly());
- }
- private async Task HandleCommandAsync(SocketMessage arg)
- {
- var message = arg as SocketUserMessage;
- if (message is null || message.Author.IsBot) return;
- int argPos = 0;
- if (message.HasStringPrefix("!", ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos))
- {
- var context = new SocketCommandContext(_client, message);
- var result = await _commands.ExecuteAsync(context, argPos, _services);
- if (!result.IsSuccess)
- Console.WriteLine(result.ErrorReason);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement