2022-11-16 21:28:05 +03:00
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
|
using Discord;
|
2022-06-06 18:39:47 +03:00
|
|
|
|
using Discord.Rest;
|
2021-12-07 21:27:27 +03:00
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
|
|
|
|
|
namespace Boyfriend;
|
|
|
|
|
|
2022-10-18 20:55:16 +03:00
|
|
|
|
public static class EventHandler {
|
|
|
|
|
private static readonly DiscordSocketClient Client = Boyfriend.Client;
|
|
|
|
|
private static bool _sendReadyMessages = true;
|
|
|
|
|
|
2022-11-16 21:28:05 +03:00
|
|
|
|
[SuppressMessage("ReSharper", "ConvertClosureToMethodGroup")]
|
2022-10-18 20:55:16 +03:00
|
|
|
|
public static void InitEvents() {
|
2022-11-16 21:28:05 +03:00
|
|
|
|
Client.Ready += () => ReadyEvent();
|
|
|
|
|
Client.MessageDeleted += (x, y) => MessageDeletedEvent(x, y);
|
|
|
|
|
Client.MessageReceived += x => MessageReceivedEvent(x);
|
|
|
|
|
Client.MessageUpdated += (x, y, z) => MessageUpdatedEvent(x, y, z);
|
|
|
|
|
Client.UserJoined += x => UserJoinedEvent(x);
|
|
|
|
|
Client.GuildScheduledEventCreated += x => ScheduledEventCreatedEvent(x);
|
|
|
|
|
Client.GuildScheduledEventCancelled += x => ScheduledEventCancelledEvent(x);
|
|
|
|
|
Client.GuildScheduledEventStarted += x => ScheduledEventStartedEvent(x);
|
|
|
|
|
Client.GuildScheduledEventCompleted += x => ScheduledEventCompletedEvent(x);
|
2021-12-10 14:25:29 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 20:55:16 +03:00
|
|
|
|
private static Task ReadyEvent() {
|
|
|
|
|
if (!_sendReadyMessages) return Task.CompletedTask;
|
2022-11-11 22:59:11 +03:00
|
|
|
|
var i = Random.Shared.Next(3);
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2022-10-18 20:55:16 +03:00
|
|
|
|
foreach (var guild in Client.Guilds) {
|
2022-05-14 16:12:24 +03:00
|
|
|
|
var config = Boyfriend.GetGuildConfig(guild.Id);
|
2022-11-11 22:59:11 +03:00
|
|
|
|
var channel = guild.GetTextChannel(Utils.ParseMention(config["BotLogChannel"]));
|
2022-05-14 16:12:24 +03:00
|
|
|
|
Utils.SetCurrentLanguage(guild.Id);
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2022-11-11 22:59:11 +03:00
|
|
|
|
if (config["ReceiveStartupMessages"] is not "true" || channel is null) continue;
|
2022-10-18 20:55:16 +03:00
|
|
|
|
_ = channel.SendMessageAsync(string.Format(Messages.Ready, Utils.GetBeep(i)));
|
2021-12-15 22:07:04 +03:00
|
|
|
|
}
|
2022-10-18 20:55:16 +03:00
|
|
|
|
|
|
|
|
|
_sendReadyMessages = false;
|
|
|
|
|
return Task.CompletedTask;
|
2021-12-07 21:27:27 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-10 14:25:29 +03:00
|
|
|
|
private static async Task MessageDeletedEvent(Cacheable<IMessage, ulong> message,
|
|
|
|
|
Cacheable<IMessageChannel, ulong> channel) {
|
2021-12-07 21:27:27 +03:00
|
|
|
|
var msg = message.Value;
|
2022-11-11 22:59:11 +03:00
|
|
|
|
if (channel.Value is not SocketGuildChannel gChannel || msg is null or ISystemMessage ||
|
|
|
|
|
msg.Author.IsBot) return;
|
2022-05-14 16:12:24 +03:00
|
|
|
|
|
2022-11-11 22:59:11 +03:00
|
|
|
|
var guild = gChannel.Guild;
|
2022-05-14 16:12:24 +03:00
|
|
|
|
|
|
|
|
|
Utils.SetCurrentLanguage(guild.Id);
|
|
|
|
|
|
2022-06-06 18:39:47 +03:00
|
|
|
|
var mention = msg.Author.Mention;
|
|
|
|
|
|
|
|
|
|
await Task.Delay(500);
|
|
|
|
|
|
2022-05-14 16:12:24 +03:00
|
|
|
|
var auditLogEntry = (await guild.GetAuditLogsAsync(1).FlattenAsync()).First();
|
2022-06-06 18:39:47 +03:00
|
|
|
|
if (auditLogEntry.Data is MessageDeleteAuditLogData data && msg.Author.Id == data.Target.Id)
|
|
|
|
|
mention = auditLogEntry.User.Mention;
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2022-11-11 21:17:44 +03:00
|
|
|
|
await Utils.SendFeedbackAsync(string.Format(Messages.CachedMessageDeleted, msg.Author.Mention,
|
|
|
|
|
Utils.MentionChannel(channel.Id),
|
|
|
|
|
Utils.Wrap(msg.CleanContent)), guild.Id, mention);
|
2021-12-07 21:27:27 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 21:46:43 +03:00
|
|
|
|
private static Task MessageReceivedEvent(SocketMessage messageParam) {
|
2022-11-11 22:59:11 +03:00
|
|
|
|
if (messageParam is not SocketUserMessage message) return Task.CompletedTask;
|
2022-05-14 16:12:24 +03:00
|
|
|
|
|
2022-10-18 21:46:43 +03:00
|
|
|
|
_ = message.CleanContent.ToLower() switch {
|
|
|
|
|
"whoami" => message.ReplyAsync("`nobody`"),
|
|
|
|
|
"сука !!" => message.ReplyAsync("`root`"),
|
|
|
|
|
"воооо" => message.ReplyAsync("`removing /...`"),
|
|
|
|
|
"op ??" => message.ReplyAsync(
|
|
|
|
|
"некоторые пасхальные цитаты которые вы могли найти были легально взяты у <@573772175572729876>"),
|
|
|
|
|
_ => new CommandProcessor(message).HandleCommandAsync()
|
|
|
|
|
};
|
|
|
|
|
return Task.CompletedTask;
|
2021-12-07 21:27:27 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task MessageUpdatedEvent(Cacheable<IMessage, ulong> messageCached, SocketMessage messageSocket,
|
|
|
|
|
ISocketMessageChannel channel) {
|
|
|
|
|
var msg = messageCached.Value;
|
2022-11-11 22:59:11 +03:00
|
|
|
|
if (channel is not SocketGuildChannel gChannel || msg is null or ISystemMessage ||
|
|
|
|
|
msg.CleanContent == messageSocket.CleanContent || msg.Author.IsBot) return;
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2022-11-11 22:59:11 +03:00
|
|
|
|
var guild = gChannel.Guild;
|
2022-10-18 20:55:16 +03:00
|
|
|
|
Utils.SetCurrentLanguage(guild.Id);
|
2022-05-14 16:12:24 +03:00
|
|
|
|
|
2022-06-06 18:39:47 +03:00
|
|
|
|
var isLimitedSpace = msg.CleanContent.Length + messageSocket.CleanContent.Length < 1940;
|
|
|
|
|
|
2022-11-11 21:17:44 +03:00
|
|
|
|
await Utils.SendFeedbackAsync(string.Format(Messages.CachedMessageEdited, Utils.MentionChannel(channel.Id),
|
2022-06-06 18:39:47 +03:00
|
|
|
|
Utils.Wrap(msg.CleanContent, isLimitedSpace), Utils.Wrap(messageSocket.CleanContent, isLimitedSpace)),
|
2022-10-18 20:55:16 +03:00
|
|
|
|
guild.Id, msg.Author.Mention);
|
2021-12-07 21:27:27 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task UserJoinedEvent(SocketGuildUser user) {
|
2021-12-10 14:25:29 +03:00
|
|
|
|
var guild = user.Guild;
|
2022-05-14 16:12:24 +03:00
|
|
|
|
var config = Boyfriend.GetGuildConfig(guild.Id);
|
2022-11-11 22:59:11 +03:00
|
|
|
|
Utils.SetCurrentLanguage(guild.Id);
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2022-07-15 19:23:45 +03:00
|
|
|
|
if (config["SendWelcomeMessages"] is "true")
|
2022-02-21 20:08:55 +03:00
|
|
|
|
await Utils.SilentSendAsync(guild.SystemChannel,
|
2022-11-13 17:40:46 +03:00
|
|
|
|
string.Format(config["WelcomeMessage"] is "default"
|
2022-11-11 22:59:11 +03:00
|
|
|
|
? Messages.DefaultWelcomeMessage
|
2022-11-13 17:40:46 +03:00
|
|
|
|
: config["WelcomeMessage"], user.Mention, guild.Name));
|
2022-05-14 16:12:24 +03:00
|
|
|
|
|
2022-11-11 21:17:44 +03:00
|
|
|
|
if (config["StarterRole"] is not "0") await user.AddRoleAsync(ulong.Parse(config["StarterRole"]));
|
2022-05-14 16:12:24 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task ScheduledEventCreatedEvent(SocketGuildEvent scheduledEvent) {
|
|
|
|
|
var guild = scheduledEvent.Guild;
|
|
|
|
|
var eventConfig = Boyfriend.GetGuildConfig(guild.Id);
|
2022-11-11 22:59:11 +03:00
|
|
|
|
var channel = Utils.GetEventNotificationChannel(guild);
|
2022-05-14 16:12:24 +03:00
|
|
|
|
|
2022-11-11 22:59:11 +03:00
|
|
|
|
if (channel is not null) {
|
|
|
|
|
var role = guild.GetRole(ulong.Parse(eventConfig["EventNotificationRole"]));
|
|
|
|
|
var mentions = role is not null
|
|
|
|
|
? $"{role.Mention} {scheduledEvent.Creator.Mention}"
|
2022-11-13 17:40:46 +03:00
|
|
|
|
: $"{scheduledEvent.Creator.Mention}";
|
2022-05-14 16:12:24 +03:00
|
|
|
|
|
2022-06-06 18:39:47 +03:00
|
|
|
|
var location = Utils.Wrap(scheduledEvent.Location) ?? Utils.MentionChannel(scheduledEvent.Channel.Id);
|
2022-11-11 22:59:11 +03:00
|
|
|
|
var descAndLink
|
2022-11-20 13:46:12 +03:00
|
|
|
|
= $"\n{Utils.Wrap(scheduledEvent.Description)}\nhttps://discord.com/events/{guild.Id}/{scheduledEvent.Id}";
|
2022-05-14 16:12:24 +03:00
|
|
|
|
|
|
|
|
|
await Utils.SilentSendAsync(channel,
|
2022-11-11 22:59:11 +03:00
|
|
|
|
string.Format(Messages.EventCreated, mentions,
|
2022-06-06 18:39:47 +03:00
|
|
|
|
Utils.Wrap(scheduledEvent.Name), location,
|
2022-11-11 22:59:11 +03:00
|
|
|
|
scheduledEvent.StartTime.ToUnixTimeSeconds().ToString(), descAndLink),
|
2022-05-14 16:12:24 +03:00
|
|
|
|
true);
|
|
|
|
|
}
|
2022-11-11 21:17:44 +03:00
|
|
|
|
|
2022-11-11 22:59:11 +03:00
|
|
|
|
if (eventConfig["EventEarlyNotificationOffset"] is not "0")
|
2022-11-11 21:17:44 +03:00
|
|
|
|
_ = Utils.SendEarlyEventStartNotificationAsync(channel, scheduledEvent,
|
2022-11-11 22:59:11 +03:00
|
|
|
|
int.Parse(eventConfig["EventEarlyNotificationOffset"]));
|
2022-05-14 16:12:24 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task ScheduledEventCancelledEvent(SocketGuildEvent scheduledEvent) {
|
|
|
|
|
var guild = scheduledEvent.Guild;
|
|
|
|
|
var eventConfig = Boyfriend.GetGuildConfig(guild.Id);
|
2022-11-11 22:59:11 +03:00
|
|
|
|
var channel = Utils.GetEventNotificationChannel(guild);
|
|
|
|
|
if (channel is not null)
|
2022-06-06 18:39:47 +03:00
|
|
|
|
await channel.SendMessageAsync(string.Format(Messages.EventCancelled, Utils.Wrap(scheduledEvent.Name),
|
2022-07-15 19:23:45 +03:00
|
|
|
|
eventConfig["FrowningFace"] is "true" ? $" {Messages.SettingsFrowningFace}" : ""));
|
2022-05-14 16:12:24 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task ScheduledEventStartedEvent(SocketGuildEvent scheduledEvent) {
|
|
|
|
|
var guild = scheduledEvent.Guild;
|
|
|
|
|
var eventConfig = Boyfriend.GetGuildConfig(guild.Id);
|
2022-11-11 22:59:11 +03:00
|
|
|
|
var channel = Utils.GetEventNotificationChannel(guild);
|
2022-05-14 16:12:24 +03:00
|
|
|
|
|
2022-11-11 22:59:11 +03:00
|
|
|
|
if (channel is not null) {
|
2022-05-14 16:12:24 +03:00
|
|
|
|
var receivers = eventConfig["EventStartedReceivers"];
|
2022-11-11 22:59:11 +03:00
|
|
|
|
var role = guild.GetRole(ulong.Parse(eventConfig["EventNotificationRole"]));
|
2022-05-14 16:12:24 +03:00
|
|
|
|
var mentions = Boyfriend.StringBuilder;
|
|
|
|
|
|
2022-11-11 22:59:11 +03:00
|
|
|
|
if (receivers.Contains("role") && role is not null) mentions.Append($"{role.Mention} ");
|
2022-05-14 16:12:24 +03:00
|
|
|
|
if (receivers.Contains("users") || receivers.Contains("interested"))
|
2022-06-06 18:39:47 +03:00
|
|
|
|
mentions = (await scheduledEvent.GetUsersAsync(15)).Aggregate(mentions,
|
|
|
|
|
(current, user) => current.Append($"{user.Mention} "));
|
2022-05-14 16:12:24 +03:00
|
|
|
|
|
|
|
|
|
await channel.SendMessageAsync(string.Format(Messages.EventStarted, mentions,
|
2022-06-06 18:39:47 +03:00
|
|
|
|
Utils.Wrap(scheduledEvent.Name),
|
|
|
|
|
Utils.Wrap(scheduledEvent.Location) ?? Utils.MentionChannel(scheduledEvent.Channel.Id)));
|
2022-05-14 16:12:24 +03:00
|
|
|
|
mentions.Clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2022-05-14 16:12:24 +03:00
|
|
|
|
private static async Task ScheduledEventCompletedEvent(SocketGuildEvent scheduledEvent) {
|
|
|
|
|
var guild = scheduledEvent.Guild;
|
2022-11-11 22:59:11 +03:00
|
|
|
|
var channel = Utils.GetEventNotificationChannel(guild);
|
|
|
|
|
if (channel is not null)
|
2022-06-06 18:39:47 +03:00
|
|
|
|
await channel.SendMessageAsync(string.Format(Messages.EventCompleted, Utils.Wrap(scheduledEvent.Name),
|
2022-11-11 22:59:11 +03:00
|
|
|
|
Utils.GetHumanizedTimeOffset(DateTimeOffset.Now.Subtract(scheduledEvent.StartTime))));
|
2021-12-07 21:27:27 +03:00
|
|
|
|
}
|
2022-09-18 17:41:29 +03:00
|
|
|
|
}
|