1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-05-04 04:56:30 +03:00

Remora.Discord part 1 out of ∞

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2023-05-16 00:11:11 +05:00
parent 02949656bf
commit 201a1ce079
Signed by: Octol1ttle
GPG key ID: B77C34313AEE1FFF
24 changed files with 141 additions and 1588 deletions

View file

@ -1,180 +1,78 @@
using System.Text;
using System.Timers;
using Boyfriend.Data;
using Discord;
using Discord.Rest;
using Discord.WebSocket;
using Timer = System.Timers.Timer;
using System.Reflection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.Caching.Extensions;
using Remora.Discord.Caching.Services;
using Remora.Discord.Gateway.Extensions;
using Remora.Discord.Hosting.Extensions;
namespace Boyfriend;
public static class Boyfriend {
public static readonly StringBuilder StringBuilder = new();
public class Boyfriend {
public static ILogger<Boyfriend> Logger = null!;
public static IConfiguration GuildConfiguration = null!;
private static readonly DiscordSocketConfig Config = new() {
MessageCacheSize = 250,
GatewayIntents
= (GatewayIntents.AllUnprivileged | GatewayIntents.MessageContent | GatewayIntents.GuildMembers)
& ~GatewayIntents.GuildInvites,
AlwaysDownloadUsers = true,
AlwaysResolveStickers = false,
AlwaysDownloadDefaultStickers = false,
LargeThreshold = 500
};
private static readonly Dictionary<string, string> ReflectionMessageCache = new();
private static DateTimeOffset _nextSongAt = DateTimeOffset.MinValue;
private static uint _nextSongIndex;
public static async Task Main(string[] args) {
var host = CreateHostBuilder(args).UseConsoleLifetime().Build();
private static readonly (Game Song, TimeSpan Duration)[] ActivityList = {
(new Game("Masayoshi Minoshima (ft. nomico) - Bad Apple!!", ActivityType.Listening), new TimeSpan(0, 3, 40)),
(new Game("Xi - Blue Zenith", ActivityType.Listening), new TimeSpan(0, 4, 16)),
(new Game("UNDEAD CORPORATION - Everything will freeze", ActivityType.Listening), new TimeSpan(0, 3, 18)),
(new Game("Splatoon 3 - Candy-Coated Rocks", ActivityType.Listening), new TimeSpan(0, 2, 39)),
(new Game("RetroSpecter - Overtime", ActivityType.Listening), new TimeSpan(0, 4, 33)),
(new Game("SOOOO - Happppy song", ActivityType.Listening), new TimeSpan(0, 5, 24))
};
var services = host.Services;
Logger = services.GetRequiredService<ILogger<Boyfriend>>();
GuildConfiguration = services.GetRequiredService<IConfigurationBuilder>().AddJsonFile("guild_configs.json")
.Build();
public static readonly DiscordSocketClient Client = new(Config);
private static readonly List<Task> GuildTickTasks = new();
private static async Task Main() {
var token = (await File.ReadAllTextAsync("token.txt")).Trim();
Client.Log += Log;
await Client.LoginAsync(TokenType.Bot, token);
await Client.StartAsync();
EventHandler.InitEvents();
var timer = new Timer();
timer.Interval = 1000;
timer.AutoReset = true;
timer.Elapsed += TickAllGuildsAsync;
if (ActivityList.Length is 0) timer.Dispose(); // CodeQL moment
timer.Start();
await Task.Delay(-1);
await host.RunAsync();
}
private static async void TickAllGuildsAsync(object? sender, ElapsedEventArgs e) {
if (GuildTickTasks.Count is not 0) return;
private static IHostBuilder CreateHostBuilder(string[] args) {
return Host.CreateDefaultBuilder(args)
.AddDiscordService(
services => {
var configuration = services.GetRequiredService<IConfiguration>();
var now = DateTimeOffset.UtcNow;
foreach (var guild in Client.Guilds) GuildTickTasks.Add(TickGuildAsync(guild, now));
return configuration.GetValue<string?>("BOT_TOKEN")
?? throw new InvalidOperationException(
"No bot token has been provided. Set the "
+ "BOT_TOKEN environment variable to a valid token.");
}
).ConfigureServices(
(_, services) => {
var responderTypes = typeof(Boyfriend).Assembly
.GetExportedTypes()
.Where(t => t.IsResponder());
foreach (var responderType in responderTypes) services.AddResponder(responderType);
if (now >= _nextSongAt) {
var nextSong = ActivityList[_nextSongIndex];
await Client.SetActivityAsync(nextSong.Song);
_nextSongAt = now.Add(nextSong.Duration);
_nextSongIndex++;
if (_nextSongIndex >= ActivityList.Length) _nextSongIndex = 0;
}
services.AddDiscordCaching();
services.Configure<CacheSettings>(
settings => { settings.SetAbsoluteExpiration<IMessage>(TimeSpan.FromDays(7)); });
try { Task.WaitAll(GuildTickTasks.ToArray()); } catch (AggregateException ex) {
foreach (var exc in ex.InnerExceptions)
await Log(
new LogMessage(
LogSeverity.Error, nameof(Boyfriend),
"Exception while ticking guilds", exc));
}
GuildTickTasks.Clear();
services.AddSingleton<IConfigurationBuilder, ConfigurationBuilder>();
}
).ConfigureLogging(
c => c.AddConsole()
.AddFilter("System.Net.Http.HttpClient.*.LogicalHandler", LogLevel.Warning)
.AddFilter("System.Net.Http.HttpClient.*.ClientHandler", LogLevel.Warning)
);
}
public 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;
public static string GetLocalized(string key) {
var propertyName = key;
key = $"{Messages.Culture}/{key}";
if (ReflectionMessageCache.TryGetValue(key, out var cached)) return cached;
case LogSeverity.Verbose:
case LogSeverity.Debug:
default: return Task.CompletedTask;
var toReturn =
typeof(Messages).GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Static)?.GetValue(null)
?.ToString();
if (toReturn is null) {
Logger.LogError("Could not find localized property: {Name}", propertyName);
return key;
}
Console.ResetColor();
return Task.CompletedTask;
}
private static async Task TickGuildAsync(SocketGuild guild, DateTimeOffset now) {
var data = GuildData.Get(guild);
var config = data.Preferences;
var saveData = false;
_ = int.TryParse(config["EventEarlyNotificationOffset"], out var offset);
foreach (var schEvent in guild.Events)
if (schEvent.Status is GuildScheduledEventStatus.Scheduled
&& config["AutoStartEvents"] is "true"
&& DateTimeOffset
.Now
>= schEvent.StartTime) await schEvent.StartAsync();
else if (!data.EarlyNotifications.Contains(schEvent.Id)
&& now >= schEvent.StartTime.Subtract(new TimeSpan(0, offset, 0))) {
data.EarlyNotifications.Add(schEvent.Id);
var receivers = config["EventStartedReceivers"];
var role = guild.GetRole(ulong.Parse(config["EventNotificationRole"]));
var mentions = StringBuilder;
if (receivers.Contains("role") && role is not null) mentions.Append($"{role.Mention} ");
if (receivers.Contains("users") || receivers.Contains("interested"))
mentions = (await schEvent.GetUsersAsync(15))
.Where(user => role is null || !((RestGuildUser)user).RoleIds.Contains(role.Id))
.Aggregate(mentions, (current, user) => current.Append($"{user.Mention} "));
await Utils.GetEventNotificationChannel(guild)?.SendMessageAsync(
string.Format(
Messages.EventEarlyNotification,
mentions,
Utils.Wrap(schEvent.Name),
schEvent.StartTime.ToUnixTimeSeconds().ToString()))!;
mentions.Clear();
}
foreach (var mData in data.MemberData.Values) {
var user = guild.GetUser(mData.Id);
if (now >= mData.BannedUntil && await guild.GetBanAsync(mData.Id) is not null)
_ = guild.RemoveBanAsync(mData.Id);
if (!mData.IsInGuild) continue;
if (mData.MutedUntil is null
&& ulong.TryParse(config["StarterRole"], out var starterRoleId)
&& guild.GetRole(starterRoleId) is not null
&& !mData.Roles.Contains(starterRoleId)) _ = user.AddRoleAsync(starterRoleId);
if (now >= mData.MutedUntil) {
saveData = await Utils.UnmuteMemberAsync(
data, Client.CurrentUser.ToString(), user,
Messages.PunishmentExpired);
}
for (var i = mData.Reminders.Count - 1; i >= 0; i--) {
var reminder = mData.Reminders[i];
if (now < reminder.RemindAt) continue;
var channel = guild.GetTextChannel(reminder.ReminderChannel);
var toSend = $"{ReplyEmojis.Reminder} {user.Mention} {Utils.Wrap(reminder.ReminderText)}";
if (channel is not null)
await channel.SendMessageAsync(toSend);
else
await Utils.SendDirectMessage(user, toSend);
mData.Reminders.RemoveAt(i);
saveData = true;
}
}
if (saveData) await data.Save(true);
ReflectionMessageCache.Add(key, toReturn);
return toReturn;
}
}