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

134 lines
4.5 KiB
C#
Raw Normal View History

2022-05-14 16:12:24 +03:00
using System.Collections.ObjectModel;
using System.Text;
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 {
2022-05-14 16:12:24 +03:00
public static readonly StringBuilder StringBuilder = new();
private static readonly Dictionary<ulong, SocketGuild> GuildCache = new();
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);
2022-05-14 16:12:24 +03:00
private static readonly Game Activity = new("Retrospecter - Genocide", ActivityType.Listening);
private static readonly Dictionary<ulong, Dictionary<string, string>> GuildConfigDictionary = new();
private static readonly Dictionary<ulong, Dictionary<ulong, ReadOnlyCollection<ulong>>> RemovedRolesDictionary =
new();
private static readonly Dictionary<string, string> EmptyGuildConfig = new();
private static readonly Dictionary<ulong, ReadOnlyCollection<ulong>> EmptyRemovedRoles = new();
public static readonly Dictionary<string, string> DefaultConfig = new() {
{"Lang", "en"},
{"Prefix", "!"},
{"RemoveRolesOnMute", "false"},
{"SendWelcomeMessages", "true"},
{"ReceiveStartupMessages", "false"},
{"FrowningFace", "true"},
{"WelcomeMessage", Messages.DefaultWelcomeMessage},
{"EventStartedReceivers", "interested,role"},
{"StarterRole", "0"},
{"MuteRole", "0"},
{"EventNotifyReceiverRole", "0"},
{"AdminLogChannel", "0"},
{"BotLogChannel", "0"},
{"EventCreatedChannel", "0"},
{"EventStartedChannel", "0"},
{"EventCancelledChannel", "0"},
{"EventCompletedChannel", "0"}
};
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-05-14 16:12:24 +03:00
await Client.SetActivityAsync(Activity);
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;
}
2022-05-14 16:12:24 +03:00
public static async Task WriteGuildConfig(ulong id) {
var json = JsonConvert.SerializeObject(GuildConfigDictionary[id], Formatting.Indented);
var removedRoles = JsonConvert.SerializeObject(RemovedRolesDictionary[id], Formatting.Indented);
2022-01-30 11:43:15 +03:00
2022-05-14 16:12:24 +03:00
await File.WriteAllTextAsync($"config_{id}.json", json);
await File.WriteAllTextAsync($"removedroles_{id}.json", removedRoles);
2021-12-15 09:19:14 +03:00
}
2022-05-14 16:12:24 +03:00
public static Dictionary<string, string> GetGuildConfig(ulong id) {
if (!RemovedRolesDictionary.ContainsKey(id))
RemovedRolesDictionary.Add(id, EmptyRemovedRoles);
if (GuildConfigDictionary.ContainsKey(id)) return GuildConfigDictionary[id];
var path = $"config_{id}.json";
if (!File.Exists(path)) File.Create(path).Dispose();
var json = File.ReadAllText(path);
var config = JsonConvert.DeserializeObject<Dictionary<string, string>>(json) ?? EmptyGuildConfig;
foreach (var key in DefaultConfig.Keys)
if (!config.ContainsKey(key))
config.Add(key, DefaultConfig[key]);
2022-02-02 16:14:26 +03:00
2022-05-14 16:12:24 +03:00
foreach (var key in config.Keys)
if (!DefaultConfig.ContainsKey(key))
config.Remove(key);
GuildConfigDictionary.Add(id, config);
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
2022-05-14 16:12:24 +03:00
public static Dictionary<ulong, ReadOnlyCollection<ulong>> GetRemovedRoles(ulong id) {
if (RemovedRolesDictionary.ContainsKey(id)) return RemovedRolesDictionary[id];
var path = $"removedroles_{id}.json";
if (!File.Exists(path)) File.Create(path);
var json = File.ReadAllText(path);
var removedRoles = JsonConvert.DeserializeObject<Dictionary<ulong, ReadOnlyCollection<ulong>>>(json) ??
EmptyRemovedRoles;
RemovedRolesDictionary.Add(id, removedRoles);
return removedRoles;
}
public static SocketGuild FindGuild(ulong channel) {
if (GuildCache.ContainsKey(channel)) return GuildCache[channel];
foreach (var guild in Client.Guilds)
2022-05-14 16:12:24 +03:00
foreach (var x in guild.Channels) {
if (x.Id != channel) continue;
GuildCache.Add(channel, guild);
return guild;
}
2021-12-15 22:07:04 +03:00
throw new Exception(Messages.CouldntFindGuildByChannel);
2021-12-15 22:07:04 +03:00
}
2022-05-14 16:12:24 +03:00
}