1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 09:09:00 +03:00
Octobot/Boyfriend/Boyfriend.cs

154 lines
5.9 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();
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.AllUnprivileged | GatewayIntents.MessageContent | GatewayIntents.GuildMembers,
2022-08-29 19:24:38 +03:00
AlwaysDownloadUsers = true,
AlwaysResolveStickers = false,
AlwaysDownloadDefaultStickers = false,
LargeThreshold = 500
2021-12-15 09:19:14 +03:00
};
2021-12-07 15:52:37 +03:00
private static readonly List<Tuple<Game, TimeSpan>> ActivityList = new() {
Tuple.Create(new Game("UNDEAD CORPORATION - Everything will freeze", ActivityType.Listening),
new TimeSpan(0, 3, 18)),
Tuple.Create(new Game("Xi - Blue Zenith", ActivityType.Listening), new TimeSpan(0, 4, 16)),
Tuple.Create(new Game("Kurokotei - Scattered Faith", ActivityType.Listening), new TimeSpan(0, 8, 21)),
Tuple.Create(new Game("Splatoon 3 - Candy-Coated Rocks", ActivityType.Listening), new TimeSpan(0, 2, 39)),
Tuple.Create(new Game("RetroSpecter - Genocide", ActivityType.Listening), new TimeSpan(0, 5, 52)),
Tuple.Create(new Game("Dimrain47 - At the Speed of Light", ActivityType.Listening), new TimeSpan(0, 4, 10))
};
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 Dictionary<ulong, Dictionary<string, string>> GuildConfigDictionary = new();
2022-05-14 16:12:24 +03:00
private static readonly Dictionary<ulong, Dictionary<ulong, ReadOnlyCollection<ulong>>> RemovedRolesDictionary =
new();
public static readonly Dictionary<string, string> DefaultConfig = new() {
{ "Prefix", "!" },
{ "Lang", "en" },
{ "ReceiveStartupMessages", "false" },
{ "WelcomeMessage", "default" },
{ "SendWelcomeMessages", "true" },
{ "BotLogChannel", "0" },
{ "StarterRole", "0" },
{ "MuteRole", "0" },
{ "RemoveRolesOnMute", "false" },
{ "FrowningFace", "true" },
{ "EventStartedReceivers", "interested,role" },
{ "EventNotificationRole", "0" },
{ "EventNotificationChannel", "0" },
{ "EventEarlyNotificationOffset", "0" }
2022-05-14 16:12:24 +03:00
};
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-10-18 20:55:16 +03:00
EventHandler.InitEvents();
2021-12-15 09:19:14 +03:00
while (true) {
foreach (var activity in ActivityList) {
await Client.SetActivityAsync(activity.Item1);
await Task.Delay(activity.Item2);
}
}
// ReSharper disable once FunctionNeverReturns
2021-12-15 09:19:14 +03:00
}
2021-12-07 15:52:37 +03:00
2021-12-15 09:19:14 +03:00
private static Task Log(LogMessage msg) {
switch (msg.Severity) {
case LogSeverity.Critical:
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.Error.WriteLine(msg.ToString());
break;
case LogSeverity.Error:
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine(msg.ToString());
break;
case LogSeverity.Warning:
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(msg.ToString());
break;
case LogSeverity.Info:
Console.WriteLine(msg.ToString());
break;
case LogSeverity.Verbose:
case LogSeverity.Debug:
default: return Task.CompletedTask;
}
2022-02-02 16:14:26 +03:00
Console.ResetColor();
2021-12-15 09:19:14 +03:00
return Task.CompletedTask;
}
public static async Task WriteGuildConfigAsync(ulong id) {
2022-05-14 16:12:24 +03:00
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 (GuildConfigDictionary.TryGetValue(id, out var cfg)) return cfg;
2022-05-14 16:12:24 +03:00
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)
?? new Dictionary<string, string>();
if (config.Keys.Count < DefaultConfig.Keys.Count) {
// ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator
// Conversion will result in a lot of memory allocations
foreach (var key in DefaultConfig.Keys)
if (!config.ContainsKey(key))
config.Add(key, DefaultConfig[key]);
} else if (config.Keys.Count > DefaultConfig.Keys.Count) {
foreach (var key in config.Keys.Where(key => !DefaultConfig.ContainsKey(key))) config.Remove(key);
}
2022-05-14 16:12:24 +03:00
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.TryGetValue(id, out var dict)) return dict;
2022-05-14 16:12:24 +03:00
var path = $"removedroles_{id}.json";
if (!File.Exists(path)) File.Create(path).Dispose();
2022-05-14 16:12:24 +03:00
var json = File.ReadAllText(path);
var removedRoles = JsonConvert.DeserializeObject<Dictionary<ulong, ReadOnlyCollection<ulong>>>(json)
?? new Dictionary<ulong, ReadOnlyCollection<ulong>>();
2022-05-14 16:12:24 +03:00
RemovedRolesDictionary.Add(id, removedRoles);
return removedRoles;
}
}