Switch to Remora.Discord (#41)

result checks go brrr

this also involves switching to using Discord's modern stuff like embeds
and interactions

and using brand-new for me programming concepts (dependency injection,
results)

---------

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
Signed-off-by: mctaylors <95250141+mctaylors@users.noreply.github.com>
Co-authored-by: mctaylors <95250141+mctaylors@users.noreply.github.com>
Co-authored-by: nrdk <neroduck@vk.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
This commit is contained in:
Octol1ttle 2023-07-09 18:32:14 +05:00 committed by GitHub
parent 2ab7a07784
commit abbb58f801
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
54 changed files with 5011 additions and 3021 deletions

View file

@ -1,180 +1,96 @@
using System.Text;
using System.Timers;
using Boyfriend.Data;
using Discord;
using Discord.Rest;
using Discord.WebSocket;
using Timer = System.Timers.Timer;
using Boyfriend.Commands;
using Boyfriend.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Remora.Commands.Extensions;
using Remora.Discord.API.Abstractions.Gateway.Commands;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Objects;
using Remora.Discord.Caching.Extensions;
using Remora.Discord.Caching.Services;
using Remora.Discord.Commands.Extensions;
using Remora.Discord.Commands.Services;
using Remora.Discord.Gateway;
using Remora.Discord.Gateway.Extensions;
using Remora.Discord.Hosting.Extensions;
using Remora.Discord.Interactivity.Extensions;
using Remora.Rest.Core;
namespace Boyfriend;
public static class Boyfriend {
public static readonly StringBuilder StringBuilder = new();
public class Boyfriend {
public static readonly AllowedMentions NoMentions = new(
Array.Empty<MentionType>(), Array.Empty<Snowflake>(), Array.Empty<Snowflake>());
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
};
public static async Task Main(string[] args) {
var host = CreateHostBuilder(args).UseConsoleLifetime().Build();
var services = host.Services;
private static DateTimeOffset _nextSongAt = DateTimeOffset.MinValue;
private static uint _nextSongIndex;
var slashService = services.GetRequiredService<SlashService>();
// Providing a guild ID to this call will result in command duplicates!
// To get rid of them, provide the ID of the guild containing duplicates,
// comment out calls to WithCommandGroup in CreateHostBuilder
// then launch the bot again and remove the guild ID
await slashService.UpdateSlashCommandsAsync();
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))
};
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) => {
services.Configure<DiscordGatewayClientOptions>(
options => options.Intents |= GatewayIntents.MessageContents
| GatewayIntents.GuildMembers
| GatewayIntents.GuildScheduledEvents);
services.Configure<CacheSettings>(
settings => {
settings.SetDefaultAbsoluteExpiration(TimeSpan.FromHours(1));
settings.SetDefaultSlidingExpiration(TimeSpan.FromMinutes(30));
settings.SetAbsoluteExpiration<IMessage>(TimeSpan.FromDays(7));
settings.SetSlidingExpiration<IMessage>(TimeSpan.FromDays(7));
});
if (now >= _nextSongAt) {
var nextSong = ActivityList[_nextSongIndex];
await Client.SetActivityAsync(nextSong.Song);
_nextSongAt = now.Add(nextSong.Duration);
_nextSongIndex++;
if (_nextSongIndex >= ActivityList.Length) _nextSongIndex = 0;
}
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();
}
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;
case LogSeverity.Verbose:
case LogSeverity.Debug:
default: return Task.CompletedTask;
}
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);
services.AddTransient<IConfigurationBuilder, ConfigurationBuilder>()
.AddDiscordCaching()
.AddDiscordCommands(true)
.AddPreparationErrorEvent<ErrorLoggingPreparationErrorEvent>()
.AddPostExecutionEvent<ErrorLoggingPostExecutionEvent>()
.AddInteractivity()
.AddInteractionGroup<InteractionResponders>()
.AddSingleton<GuildDataService>()
.AddSingleton<UtilityService>()
.AddHostedService<GuildUpdateService>()
.AddCommandTree()
.WithCommandGroup<AboutCommandGroup>()
.WithCommandGroup<BanCommandGroup>()
.WithCommandGroup<ClearCommandGroup>()
.WithCommandGroup<KickCommandGroup>()
.WithCommandGroup<MuteCommandGroup>()
.WithCommandGroup<PingCommandGroup>()
.WithCommandGroup<RemindCommandGroup>()
.WithCommandGroup<SettingsCommandGroup>();
var responderTypes = typeof(Boyfriend).Assembly
.GetExportedTypes()
.Where(t => t.IsResponder());
foreach (var responderType in responderTypes) services.AddResponder(responderType);
}
).ConfigureLogging(
c => c.AddConsole()
.AddFilter("System.Net.Http.HttpClient.*.LogicalHandler", LogLevel.Warning)
.AddFilter("System.Net.Http.HttpClient.*.ClientHandler", LogLevel.Warning)
);
}
}