dotbot/Program.cs

81 lines
2.5 KiB
C#

using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Reflection;
using System.Threading.Tasks;
namespace dotbot
{
public class Program
{
private CommandService _commands;
private DiscordSocketClient _client;
private IConfigurationRoot _config;
private IServiceProvider _services;
public static void Main(string[] args)
=> new Program().StartAsync().GetAwaiter().GetResult();
public async Task StartAsync()
{
var builder = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("_config.json");
_config = builder.Build();
// get token from _config.json file
// if your bot isn't connecting, rename _config.example.json to _config.json
// and then place your bot token between the quotes
string token = _config["tokens:discord"];
Console.WriteLine(token);
_client = new DiscordSocketClient();
_commands = new CommandService();
_client.Log += Log;
_services = new ServiceCollection()
.AddSingleton(_client)
.AddSingleton(_commands)
.AddSingleton(_config)
.AddSingleton<Random>()
.BuildServiceProvider();
// install all commands from the assembly
_client.MessageReceived += HandleCommandAsync;
await _commands.AddModulesAsync(Assembly.GetEntryAssembly());
await _client.LoginAsync(TokenType.Bot, token);
await _client.StartAsync();
await Task.Delay(-1);
}
private async Task HandleCommandAsync(SocketMessage arg)
{
var message = arg as SocketUserMessage;
if (message == null) return;
int argPos = 0;
if (!(message.HasStringPrefix(_config["prefix"], ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos))) return;
var context = new SocketCommandContext(_client, message);
var result = await _commands.ExecuteAsync(context, argPos, _services);
if (!result.IsSuccess)
await context.Channel.SendMessageAsync(result.ErrorReason);
}
private Task Log(LogMessage msg)
{
Console.WriteLine(msg.ToString());
return Task.CompletedTask;
}
}
}