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

74 lines
2.4 KiB
C#
Raw Normal View History

using System.Globalization;
using System.Text.Json;
2021-12-15 09:19:14 +03:00
using Discord;
2021-12-07 15:52:37 +03:00
using Discord.WebSocket;
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() {
Client.Log += Log;
var token = (await File.ReadAllTextAsync("token.txt")).Trim();
await Client.LoginAsync(TokenType.Bot, token);
await Client.StartAsync();
await Client.SetActivityAsync(new Game("Retrospecter - Chiller", 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());
return Task.CompletedTask;
}
public static async Task SetupGuildConfigs() {
foreach (var guild in Client.Guilds) {
var path = "config_" + guild.Id + ".json";
var openStream = !File.Exists(path) ? File.Create(path) : File.OpenRead(path);
GuildConfig config;
try {
config = await JsonSerializer.DeserializeAsync<GuildConfig>(openStream) ?? throw new Exception();
} catch (JsonException) {
Messages.Culture = new CultureInfo("ru");
config = new GuildConfig(guild.Id, "ru", "!", false, true,
true, Messages.DefaultWelcomeMessage, 0, 0, 0, 0);
2021-12-15 09:19:14 +03:00
}
GuildConfigDictionary.Add(guild.Id, 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");
2021-12-15 22:07:04 +03:00
var toReturn = GuildConfigDictionary.ContainsKey(guild.Id) ? GuildConfigDictionary[guild.Id]
: new GuildConfig(guild.Id, "ru", "!", false, true, true, Messages.DefaultWelcomeMessage, 0, 0, 0, 0);
2021-12-15 09:19:14 +03:00
if (toReturn.Id != guild.Id) throw new Exception();
return toReturn;
}
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
}
2021-12-15 09:19:14 +03:00
}