This repository has been archived on 2024-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
OctobotStealth/Boyfriend/Boyfriend.cs

76 lines
2.2 KiB
C#
Raw Normal View History

using System.Globalization;
2021-12-15 09:19:14 +03:00
using Discord;
2021-12-07 15:52:37 +03:00
using Discord.WebSocket;
using Newtonsoft.Json;
2021-12-07 15:52:37 +03:00
namespace Boyfriend;
2021-12-15 09:19:14 +03:00
public static class Boyfriend {
2021-12-07 15:52:37 +03:00
2021-12-15 09:19:14 +03:00
private static readonly DiscordSocketConfig Config = new() {
MessageCacheSize = 250,
GatewayIntents = GatewayIntents.All
};
2021-12-07 15:52:37 +03:00
2021-12-15 09:19:14 +03:00
public static readonly DiscordSocketClient Client = new(Config);
2021-12-07 15:52:37 +03:00
2021-12-15 09:19:14 +03:00
private static readonly Dictionary<ulong, GuildConfig> GuildConfigDictionary = new();
2021-12-07 15:52:37 +03:00
public static void Main() {
Init().GetAwaiter().GetResult();
}
2021-12-15 09:19:14 +03:00
private static async Task Init() {
var token = (await File.ReadAllTextAsync("token.txt")).Trim();
2022-02-02 16:14:26 +03:00
Client.Log += Log;
2021-12-15 09:19:14 +03:00
await Client.LoginAsync(TokenType.Bot, token);
await Client.StartAsync();
2022-01-30 11:43:15 +03:00
await Client.SetActivityAsync(new Game("Retrospecter - Expurgation", ActivityType.Listening));
2021-12-15 09:19:14 +03:00
new EventHandler().InitEvents();
2021-12-15 09:19:14 +03:00
await Task.Delay(-1);
}
2021-12-07 15:52:37 +03:00
2021-12-15 09:19:14 +03:00
private static Task Log(LogMessage msg) {
Console.WriteLine(msg.ToString());
2022-02-02 16:14:26 +03:00
2021-12-15 09:19:14 +03:00
return Task.CompletedTask;
}
public static async Task SetupGuildConfigs() {
foreach (var guild in Client.Guilds) {
var path = "config_" + guild.Id + ".json";
2022-01-30 11:43:15 +03:00
if (!File.Exists(path)) File.Create(path);
2021-12-15 09:19:14 +03:00
2022-01-30 11:43:15 +03:00
var config = JsonConvert.DeserializeObject<GuildConfig>(await File.ReadAllTextAsync(path));
if (config == null) {
Messages.Culture = new CultureInfo("ru");
2022-01-30 11:43:15 +03:00
config = new GuildConfig(guild.Id);
2021-12-15 09:19:14 +03:00
}
2022-01-30 11:43:15 +03:00
config.Validate();
GuildConfigDictionary.Add(config.Id.GetValueOrDefault(0), config);
2021-12-07 15:52:37 +03:00
}
2021-12-15 09:19:14 +03:00
}
public static GuildConfig GetGuildConfig(IGuild guild) {
Messages.Culture = new CultureInfo("ru");
2022-02-02 16:14:26 +03:00
var config = GuildConfigDictionary.ContainsKey(guild.Id) ? GuildConfigDictionary[guild.Id]
: new GuildConfig(guild.Id);
2022-01-30 11:43:15 +03:00
config.Validate();
2021-12-15 09:19:14 +03:00
2022-01-30 11:43:15 +03:00
return config;
2021-12-15 09:19:14 +03:00
}
2021-12-15 22:07:04 +03:00
public static IGuild FindGuild(IMessageChannel channel) {
foreach (var guild in Client.Guilds)
if (guild.Channels.Any(x => x == channel))
return guild;
2021-12-15 22:07:04 +03:00
throw new Exception(Messages.CouldntFindGuildByChannel);
2021-12-15 22:07:04 +03:00
}
}