mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-05-06 22:16:29 +03:00
another two or three refactors
This commit is contained in:
parent
868b6bcaa7
commit
790f77aa49
21 changed files with 1447 additions and 944 deletions
|
@ -1,4 +1,5 @@
|
|||
using System.Globalization;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Text;
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
using Newtonsoft.Json;
|
||||
|
@ -6,6 +7,8 @@ using Newtonsoft.Json;
|
|||
namespace Boyfriend;
|
||||
|
||||
public static class Boyfriend {
|
||||
public static readonly StringBuilder StringBuilder = new();
|
||||
private static readonly Dictionary<ulong, SocketGuild> GuildCache = new();
|
||||
|
||||
private static readonly DiscordSocketConfig Config = new() {
|
||||
MessageCacheSize = 250,
|
||||
|
@ -13,8 +16,34 @@ public static class Boyfriend {
|
|||
};
|
||||
|
||||
public static readonly DiscordSocketClient Client = new(Config);
|
||||
private static readonly Game Activity = new("Retrospecter - Genocide", ActivityType.Listening);
|
||||
|
||||
private static readonly Dictionary<ulong, GuildConfig> GuildConfigDictionary = new();
|
||||
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"}
|
||||
};
|
||||
|
||||
public static void Main() {
|
||||
Init().GetAwaiter().GetResult();
|
||||
|
@ -27,7 +56,7 @@ public static class Boyfriend {
|
|||
|
||||
await Client.LoginAsync(TokenType.Bot, token);
|
||||
await Client.StartAsync();
|
||||
await Client.SetActivityAsync(new Game("Retrospecter - Expurgation", ActivityType.Listening));
|
||||
await Client.SetActivityAsync(Activity);
|
||||
|
||||
new EventHandler().InitEvents();
|
||||
|
||||
|
@ -40,37 +69,65 @@ public static class Boyfriend {
|
|||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public static async Task SetupGuildConfigs() {
|
||||
foreach (var guild in Client.Guilds) {
|
||||
var path = "config_" + guild.Id + ".json";
|
||||
if (!File.Exists(path)) File.Create(path);
|
||||
public static async Task WriteGuildConfig(ulong id) {
|
||||
var json = JsonConvert.SerializeObject(GuildConfigDictionary[id], Formatting.Indented);
|
||||
var removedRoles = JsonConvert.SerializeObject(RemovedRolesDictionary[id], Formatting.Indented);
|
||||
|
||||
var config = JsonConvert.DeserializeObject<GuildConfig>(await File.ReadAllTextAsync(path));
|
||||
if (config == null) {
|
||||
Messages.Culture = new CultureInfo("ru");
|
||||
config = new GuildConfig(guild.Id);
|
||||
}
|
||||
config.Validate();
|
||||
|
||||
GuildConfigDictionary.Add(config.Id.GetValueOrDefault(0), config);
|
||||
}
|
||||
await File.WriteAllTextAsync($"config_{id}.json", json);
|
||||
await File.WriteAllTextAsync($"removedroles_{id}.json", removedRoles);
|
||||
}
|
||||
|
||||
public static GuildConfig GetGuildConfig(IGuild guild) {
|
||||
Messages.Culture = new CultureInfo("ru");
|
||||
public static Dictionary<string, string> GetGuildConfig(ulong id) {
|
||||
if (!RemovedRolesDictionary.ContainsKey(id))
|
||||
RemovedRolesDictionary.Add(id, EmptyRemovedRoles);
|
||||
|
||||
var config = GuildConfigDictionary.ContainsKey(guild.Id) ? GuildConfigDictionary[guild.Id]
|
||||
: new GuildConfig(guild.Id);
|
||||
config.Validate();
|
||||
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]);
|
||||
|
||||
foreach (var key in config.Keys)
|
||||
if (!DefaultConfig.ContainsKey(key))
|
||||
config.Remove(key);
|
||||
|
||||
GuildConfigDictionary.Add(id, config);
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
public static IGuild FindGuild(IMessageChannel channel) {
|
||||
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)
|
||||
if (guild.Channels.Any(x => x == channel))
|
||||
return guild;
|
||||
foreach (var x in guild.Channels) {
|
||||
if (x.Id != channel) continue;
|
||||
GuildCache.Add(channel, guild);
|
||||
return guild;
|
||||
}
|
||||
|
||||
throw new Exception(Messages.CouldntFindGuildByChannel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue