initial
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KasinoBot
|
||||
{
|
||||
public class Client
|
||||
{
|
||||
private static DiscordSocketClient? client;
|
||||
private CommandService? commandService;
|
||||
private ServiceProvider? services;
|
||||
|
||||
private async Task MessageReceived (SocketMessage rawMessage)
|
||||
{
|
||||
if (commandService == null || services == null)
|
||||
return;
|
||||
|
||||
if (rawMessage is not SocketUserMessage message)
|
||||
return;
|
||||
|
||||
if (message.Source != MessageSource.User)
|
||||
return;
|
||||
|
||||
var argPos = 0;
|
||||
if (!message.HasCharPrefix('!', ref argPos))
|
||||
return;
|
||||
|
||||
var context = new SocketCommandContext(client, message);
|
||||
await commandService.ExecuteAsync(context, argPos, services);
|
||||
}
|
||||
|
||||
public async Task Initialize()
|
||||
{
|
||||
Console.WriteLine("Initializing...");
|
||||
|
||||
using (services = GetServices())
|
||||
{
|
||||
// Initialize Discord Socket Client and Command Service
|
||||
client = services.GetRequiredService<DiscordSocketClient>();
|
||||
commandService = services.GetRequiredService<CommandService>();
|
||||
|
||||
// add log handlers
|
||||
client.Log += LogAsync;
|
||||
client.MessageReceived += MessageReceived;
|
||||
client.Ready += ReadyAsync;
|
||||
|
||||
services.GetRequiredService<CommandService>().Log += LogAsync;
|
||||
|
||||
// Start Discord Socket Client
|
||||
await client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("TOKEN"));
|
||||
await client.StartAsync();
|
||||
|
||||
// Initialize command handler
|
||||
await services.GetRequiredService<CommandHandler>().InitializeAsync();
|
||||
|
||||
// Hang there!
|
||||
await Task.Delay(Timeout.Infinite);
|
||||
}
|
||||
}
|
||||
|
||||
private ServiceProvider GetServices()
|
||||
{
|
||||
return new ServiceCollection()
|
||||
.AddSingleton<DiscordSocketClient>()
|
||||
.AddSingleton<CommandService>()
|
||||
.AddSingleton<CommandHandler>()
|
||||
.BuildServiceProvider();
|
||||
}
|
||||
|
||||
private Task ReadyAsync()
|
||||
{
|
||||
Console.WriteLine($"{client?.CurrentUser} is connected!");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task LogAsync(LogMessage arg)
|
||||
{
|
||||
Console.WriteLine(arg.ToString());
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user