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

183 lines
8.3 KiB
C#
Raw Normal View History

2022-10-18 20:55:16 +03:00
using Discord;
using Discord.Rest;
2021-12-07 21:27:27 +03:00
using Discord.WebSocket;
2022-10-18 20:55:16 +03:00
using Humanizer;
2021-12-07 21:27:27 +03:00
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;
public static void InitEvents() {
Client.Ready += ReadyEvent;
Client.MessageDeleted += MessageDeletedEvent;
Client.MessageReceived += MessageReceivedEvent;
Client.MessageUpdated += MessageUpdatedEvent;
Client.UserJoined += UserJoinedEvent;
Client.GuildScheduledEventCreated += ScheduledEventCreatedEvent;
Client.GuildScheduledEventCancelled += ScheduledEventCancelledEvent;
Client.GuildScheduledEventStarted += ScheduledEventStartedEvent;
Client.GuildScheduledEventCompleted += ScheduledEventCompletedEvent;
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-05-14 16:12:24 +03:00
var i = Utils.Random.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);
var channel = guild.GetTextChannel(Convert.ToUInt64(config["BotLogChannel"]));
Utils.SetCurrentLanguage(guild.Id);
2022-02-02 16:14:26 +03:00
2022-10-18 20:55:16 +03:00
if (config["ReceiveStartupMessages"] is not "true" || channel == null ||
Utils.IsServerBlacklisted(guild)) continue;
_ = 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-05-14 16:12:24 +03:00
if (msg is null or ISystemMessage || msg.Author.IsBot) return;
var guild = Boyfriend.FindGuild(channel.Value.Id);
2022-10-18 20:55:16 +03:00
if (Utils.IsServerBlacklisted(guild)) return;
2022-05-14 16:12:24 +03:00
Utils.SetCurrentLanguage(guild.Id);
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();
if (auditLogEntry.Data is MessageDeleteAuditLogData data && msg.Author.Id == data.Target.Id)
mention = auditLogEntry.User.Mention;
2022-02-02 16:14:26 +03:00
await Utils.SendFeedbackAsync(
2022-05-14 16:12:24 +03:00
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
}
private static Task MessageReceivedEvent(SocketMessage messageParam) {
if (messageParam is not SocketUserMessage { Author: SocketGuildUser user } message) return Task.CompletedTask;
2022-02-02 16:14:26 +03:00
2021-12-07 21:27:27 +03:00
var guild = user.Guild;
2022-05-14 16:12:24 +03:00
Utils.SetCurrentLanguage(guild.Id);
_ = 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-02-02 16:14:26 +03:00
2022-05-14 16:12:24 +03:00
if (msg is null or ISystemMessage || msg.CleanContent == messageSocket.CleanContent || msg.Author.IsBot) return;
2022-02-02 16:14:26 +03:00
2022-10-18 20:55:16 +03:00
var guild = Boyfriend.FindGuild(channel.Id);
if (Utils.IsServerBlacklisted(guild)) return;
2022-05-14 16:12:24 +03:00
2022-10-18 20:55:16 +03:00
Utils.SetCurrentLanguage(guild.Id);
2022-05-14 16:12:24 +03:00
var isLimitedSpace = msg.CleanContent.Length + messageSocket.CleanContent.Length < 1940;
await Utils.SendFeedbackAsync(
2022-05-14 16:12:24 +03:00
string.Format(Messages.CachedMessageEdited, Utils.MentionChannel(channel.Id),
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-10-18 20:55:16 +03:00
if (Utils.IsServerBlacklisted(guild)) return;
2022-05-14 16:12:24 +03:00
var config = Boyfriend.GetGuildConfig(guild.Id);
2022-02-02 16:14:26 +03:00
if (config["SendWelcomeMessages"] is "true")
await Utils.SilentSendAsync(guild.SystemChannel,
2022-05-14 16:12:24 +03:00
string.Format(config["WelcomeMessage"], user.Mention, guild.Name));
if (config["StarterRole"] is not "0")
2022-05-14 16:12:24 +03:00
await user.AddRoleAsync(ulong.Parse(config["StarterRole"]));
}
private static async Task ScheduledEventCreatedEvent(SocketGuildEvent scheduledEvent) {
var guild = scheduledEvent.Guild;
2022-10-18 20:55:16 +03:00
if (Utils.IsServerBlacklisted(guild)) return;
2022-05-14 16:12:24 +03:00
var eventConfig = Boyfriend.GetGuildConfig(guild.Id);
var channel = guild.GetTextChannel(Convert.ToUInt64(eventConfig["EventCreatedChannel"]));
if (channel != null) {
var roleMention = "";
var role = guild.GetRole(Convert.ToUInt64(eventConfig["EventNotifyReceiverRole"]));
if (role != null)
roleMention = $"{role.Mention} ";
var location = Utils.Wrap(scheduledEvent.Location) ?? Utils.MentionChannel(scheduledEvent.Channel.Id);
2022-05-14 16:12:24 +03:00
await Utils.SilentSendAsync(channel,
string.Format(Messages.EventCreated, "\n", roleMention, scheduledEvent.Creator.Mention,
Utils.Wrap(scheduledEvent.Name), location,
scheduledEvent.StartTime.ToUnixTimeSeconds().ToString(), Utils.Wrap(scheduledEvent.Description), guild.Id, scheduledEvent.Id),
2022-05-14 16:12:24 +03:00
true);
}
if (eventConfig["EventEarlyNotificationOffset"] != "0") {
_ = Utils.SendEarlyEventStartNotificationAsync(channel, scheduledEvent, Convert.ToInt32(eventConfig["EventEarlyNotificationOffset"]));
}
2022-05-14 16:12:24 +03:00
}
private static async Task ScheduledEventCancelledEvent(SocketGuildEvent scheduledEvent) {
var guild = scheduledEvent.Guild;
2022-10-18 20:55:16 +03:00
if (Utils.IsServerBlacklisted(guild)) return;
2022-05-14 16:12:24 +03:00
var eventConfig = Boyfriend.GetGuildConfig(guild.Id);
var channel = guild.GetTextChannel(Convert.ToUInt64(eventConfig["EventCancelledChannel"]));
if (channel != null)
await channel.SendMessageAsync(string.Format(Messages.EventCancelled, Utils.Wrap(scheduledEvent.Name),
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;
2022-10-18 20:55:16 +03:00
if (Utils.IsServerBlacklisted(guild)) return;
2022-05-14 16:12:24 +03:00
var eventConfig = Boyfriend.GetGuildConfig(guild.Id);
var channel = guild.GetTextChannel(Convert.ToUInt64(eventConfig["EventStartedChannel"]));
if (channel != null) {
var receivers = eventConfig["EventStartedReceivers"];
var role = guild.GetRole(Convert.ToUInt64(eventConfig["EventNotifyReceiverRole"]));
var mentions = Boyfriend.StringBuilder;
if (receivers.Contains("role") && role != null) mentions.Append($"{role.Mention} ");
if (receivers.Contains("users") || receivers.Contains("interested"))
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,
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-10-18 20:55:16 +03:00
if (Utils.IsServerBlacklisted(guild)) return;
2022-05-14 16:12:24 +03:00
var eventConfig = Boyfriend.GetGuildConfig(guild.Id);
var channel = guild.GetTextChannel(Convert.ToUInt64(eventConfig["EventCompletedChannel"]));
if (channel != null)
await channel.SendMessageAsync(string.Format(Messages.EventCompleted, Utils.Wrap(scheduledEvent.Name),
2022-10-18 20:55:16 +03:00
Utils.Wrap(scheduledEvent.StartTime.Subtract(DateTimeOffset.Now).Negate().Humanize())));
2021-12-07 21:27:27 +03:00
}
}