1
0
This repository has been archived on 2023-08-03. You can view files and clone it, but cannot push or open issues or pull requests.
budet-cho-bot/Budet-cho-bot/Program.cs
Dmitrii Kollerov 784ece36cd initial
2023-07-14 20:19:56 +07:00

91 lines
2.8 KiB
C#

using Discord;
using Discord.Commands;
using Discord.Net;
using Discord.WebSocket;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Reflection;
using System.Threading.Tasks;
using Discord_Bot;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class Program
{
private DiscordSocketClient _client;
private readonly CommandService _commands;
private readonly IServiceProvider _services;
public static Task Main(string[] args) => new Program().MainAsync();
public async Task MainAsync()
{
using var services = ConfigureServices();
Console.WriteLine("Ready for takeoff...");
var client = services.GetRequiredService<DiscordSocketClient>();
client.Log += Log;
services.GetRequiredService<CommandService>().Log += Log;
// Get the bot token from the Config.json file.
JObject config = Functions.GetConfig();
string token = config["token"].Value<string>();
// Log in to Discord and start the bot.
await client.LoginAsync(TokenType.Bot, token);
await client.StartAsync();
await services.GetRequiredService<CommandHandlingService>().InitializeAsync();
// Run the bot forever.
await Task.Delay(-1);
}
public ServiceProvider ConfigureServices()
{
return new ServiceCollection()
.AddSingleton(new DiscordSocketClient(new DiscordSocketConfig
{
MessageCacheSize = 500,
LogLevel = LogSeverity.Info,
GatewayIntents = GatewayIntents.All,
}))
.AddSingleton(new CommandService(new CommandServiceConfig
{
LogLevel = LogSeverity.Info,
DefaultRunMode = RunMode.Async,
CaseSensitiveCommands = false
}))
.AddSingleton<CommandHandlingService>()
.BuildServiceProvider();
}
private Task Log(LogMessage message)
{
switch (message.Severity)
{
case LogSeverity.Critical:
case LogSeverity.Error:
Console.ForegroundColor = ConsoleColor.Red;
break;
case LogSeverity.Warning:
Console.ForegroundColor = ConsoleColor.Yellow;
break;
case LogSeverity.Info:
Console.ForegroundColor = ConsoleColor.White;
break;
case LogSeverity.Verbose:
case LogSeverity.Debug:
Console.ForegroundColor = ConsoleColor.DarkGray;
break;
}
Console.WriteLine($"{DateTime.Now,-19} [{message.Severity,8}] {message.Source}: {message.Message} {message.Exception}");
Console.ResetColor();
return Task.CompletedTask;
}
}