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;
|
2022-02-21 20:08:55 +03:00
|
|
|
|
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,
|
2022-11-12 09:02:44 +03:00
|
|
|
|
GatewayIntents = (GatewayIntents.AllUnprivileged | GatewayIntents.MessageContent | GatewayIntents.GuildMembers) & ~GatewayIntents.GuildInvites,
|
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
|
|
|
|
|
2022-10-22 19:25:43 +03:00
|
|
|
|
private static readonly List<Tuple<Game, TimeSpan>> ActivityList = new() {
|
2022-10-25 20:57:25 +03:00
|
|
|
|
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))
|
2022-10-22 19:25:43 +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 Dictionary<ulong, Dictionary<string, string>> GuildConfigDictionary = new();
|
2022-06-06 18:39:47 +03:00
|
|
|
|
|
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() {
|
2022-06-06 18:39:47 +03:00
|
|
|
|
{ "Prefix", "!" },
|
2022-11-11 22:59:11 +03:00
|
|
|
|
{ "Lang", "en" },
|
2022-06-06 18:39:47 +03:00
|
|
|
|
{ "ReceiveStartupMessages", "false" },
|
2022-11-11 22:59:11 +03:00
|
|
|
|
{ "WelcomeMessage", "default" },
|
|
|
|
|
{ "SendWelcomeMessages", "true" },
|
|
|
|
|
{ "BotLogChannel", "0" },
|
2022-06-06 18:39:47 +03:00
|
|
|
|
{ "StarterRole", "0" },
|
|
|
|
|
{ "MuteRole", "0" },
|
2022-11-11 22:59:11 +03:00
|
|
|
|
{ "RemoveRolesOnMute", "false" },
|
|
|
|
|
{ "FrowningFace", "true" },
|
|
|
|
|
{ "EventStartedReceivers", "interested,role" },
|
|
|
|
|
{ "EventNotificationRole", "0" },
|
|
|
|
|
{ "EventNotificationChannel", "0" },
|
2022-10-23 12:49:49 +03:00
|
|
|
|
{ "EventEarlyNotificationOffset", "0" }
|
2022-05-14 16:12:24 +03:00
|
|
|
|
};
|
2021-12-07 15:52:37 +03:00
|
|
|
|
|
2022-01-18 20:38:15 +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
|
|
|
|
|
2022-10-22 19:25:43 +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) {
|
2022-11-11 22:59:11 +03:00
|
|
|
|
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
|
|
|
|
|
2022-11-11 22:59:11 +03:00
|
|
|
|
Console.ResetColor();
|
2021-12-15 09:19:14 +03:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-18 17:41:29 +03:00
|
|
|
|
public static async Task WriteGuildConfigAsync(ulong id) {
|
2022-11-12 09:02:44 +03:00
|
|
|
|
await File.WriteAllTextAsync($"config_{id}.json", JsonConvert.SerializeObject(GuildConfigDictionary[id], Formatting.Indented));
|
2022-01-30 11:43:15 +03:00
|
|
|
|
|
2022-11-12 09:02:44 +03:00
|
|
|
|
if (RemovedRolesDictionary.TryGetValue(id, out var removedRoles))
|
|
|
|
|
await File.WriteAllTextAsync($"removedroles_{id}.json",
|
|
|
|
|
JsonConvert.SerializeObject(removedRoles, Formatting.Indented));
|
2021-12-15 09:19:14 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-14 16:12:24 +03:00
|
|
|
|
public static Dictionary<string, string> GetGuildConfig(ulong id) {
|
2022-10-21 09:09:56 +03:00
|
|
|
|
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);
|
2022-06-06 18:39:47 +03:00
|
|
|
|
var config = JsonConvert.DeserializeObject<Dictionary<string, string>>(json)
|
|
|
|
|
?? new Dictionary<string, string>();
|
|
|
|
|
|
|
|
|
|
if (config.Keys.Count < DefaultConfig.Keys.Count) {
|
|
|
|
|
// ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator
|
2022-11-11 22:59:11 +03:00
|
|
|
|
// Conversion will result in a lot of memory allocations
|
2022-06-06 18:39:47 +03:00
|
|
|
|
foreach (var key in DefaultConfig.Keys)
|
|
|
|
|
if (!config.ContainsKey(key))
|
|
|
|
|
config.Add(key, DefaultConfig[key]);
|
|
|
|
|
} else if (config.Keys.Count > DefaultConfig.Keys.Count) {
|
2022-11-11 22:59:11 +03:00
|
|
|
|
foreach (var key in config.Keys.Where(key => !DefaultConfig.ContainsKey(key))) config.Remove(key);
|
2022-06-06 18:39:47 +03:00
|
|
|
|
}
|
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) {
|
2022-10-21 09:09:56 +03:00
|
|
|
|
if (RemovedRolesDictionary.TryGetValue(id, out var dict)) return dict;
|
2022-05-14 16:12:24 +03:00
|
|
|
|
var path = $"removedroles_{id}.json";
|
|
|
|
|
|
2022-11-11 22:59:11 +03:00
|
|
|
|
if (!File.Exists(path)) File.Create(path).Dispose();
|
2022-05-14 16:12:24 +03:00
|
|
|
|
|
|
|
|
|
var json = File.ReadAllText(path);
|
2022-06-06 18:39:47 +03:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|