2022-05-14 16:12:24 +03:00
|
|
|
|
using Boyfriend.Commands;
|
2021-12-07 21:27:27 +03:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
public class EventHandler {
|
|
|
|
|
private readonly DiscordSocketClient _client = Boyfriend.Client;
|
|
|
|
|
|
2022-01-18 20:38:15 +03:00
|
|
|
|
public void InitEvents() {
|
2021-12-07 21:27:27 +03:00
|
|
|
|
_client.Ready += ReadyEvent;
|
|
|
|
|
_client.MessageDeleted += MessageDeletedEvent;
|
|
|
|
|
_client.MessageReceived += MessageReceivedEvent;
|
|
|
|
|
_client.MessageUpdated += MessageUpdatedEvent;
|
|
|
|
|
_client.UserJoined += UserJoinedEvent;
|
2022-05-14 16:12:24 +03:00
|
|
|
|
_client.GuildScheduledEventCreated += ScheduledEventCreatedEvent;
|
|
|
|
|
_client.GuildScheduledEventCancelled += ScheduledEventCancelledEvent;
|
|
|
|
|
_client.GuildScheduledEventStarted += ScheduledEventStartedEvent;
|
|
|
|
|
_client.GuildScheduledEventCompleted += ScheduledEventCompletedEvent;
|
2021-12-10 14:25:29 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 22:07:04 +03:00
|
|
|
|
private static async Task ReadyEvent() {
|
2022-05-14 16:12:24 +03:00
|
|
|
|
var i = Utils.Random.Next(3);
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2021-12-15 22:07:04 +03:00
|
|
|
|
foreach (var guild in Boyfriend.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-07-15 19:23:45 +03:00
|
|
|
|
if (config["ReceiveStartupMessages"] is not "true" || channel == null) continue;
|
2022-05-14 16:12:24 +03:00
|
|
|
|
await channel.SendMessageAsync(string.Format(Messages.Ready, Utils.GetBeep(i)));
|
2021-12-15 22:07:04 +03:00
|
|
|
|
}
|
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);
|
|
|
|
|
|
|
|
|
|
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-05-14 16:12:24 +03:00
|
|
|
|
await Utils.SendFeedback(
|
|
|
|
|
string.Format(Messages.CachedMessageDeleted, msg.Author.Mention, Utils.MentionChannel(channel.Id),
|
2022-06-06 18:39:47 +03:00
|
|
|
|
Utils.Wrap(msg.CleanContent)), guild.Id, mention);
|
2021-12-07 21:27:27 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 09:19:14 +03:00
|
|
|
|
private static async Task MessageReceivedEvent(SocketMessage messageParam) {
|
2022-07-15 16:32:54 +03:00
|
|
|
|
if (messageParam is not SocketUserMessage { Author: SocketGuildUser user } message) return;
|
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);
|
|
|
|
|
|
|
|
|
|
if ((message.MentionedUsers.Count > 3 || message.MentionedRoles.Count > 2) &&
|
|
|
|
|
!user.GuildPermissions.MentionEveryone) {
|
2022-08-30 18:15:01 +03:00
|
|
|
|
await BanCommand.BanUser(new CommandProcessor(message), user, TimeSpan.FromMilliseconds(-1),
|
2022-05-14 16:12:24 +03:00
|
|
|
|
Messages.AutobanReason);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 16:14:26 +03:00
|
|
|
|
var prev = "";
|
|
|
|
|
var prevFailsafe = "";
|
|
|
|
|
var prevs = await message.Channel.GetMessagesAsync(3).FlattenAsync();
|
|
|
|
|
var prevsArray = prevs as IMessage[] ?? prevs.ToArray();
|
|
|
|
|
|
2022-05-14 16:12:24 +03:00
|
|
|
|
if (prevsArray.Length >= 3) {
|
2022-01-18 20:38:15 +03:00
|
|
|
|
prev = prevsArray[1].Content;
|
|
|
|
|
prevFailsafe = prevsArray[2].Content;
|
2022-05-14 16:12:24 +03:00
|
|
|
|
}
|
2022-01-18 20:38:15 +03:00
|
|
|
|
|
2022-08-05 19:01:06 +03:00
|
|
|
|
if (user == guild.CurrentUser || (user.IsBot &&
|
|
|
|
|
(message.Content.Contains(prev) || message.Content.Contains(prevFailsafe))))
|
2021-12-07 21:27:27 +03:00
|
|
|
|
return;
|
|
|
|
|
|
2022-08-30 18:15:01 +03:00
|
|
|
|
_ = new CommandProcessor(message).HandleCommand();
|
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-05-14 16:12:24 +03:00
|
|
|
|
var guildId = Boyfriend.FindGuild(channel.Id).Id;
|
|
|
|
|
|
|
|
|
|
Utils.SetCurrentLanguage(guildId);
|
|
|
|
|
|
2022-06-06 18:39:47 +03:00
|
|
|
|
var isLimitedSpace = msg.CleanContent.Length + messageSocket.CleanContent.Length < 1940;
|
|
|
|
|
|
2022-05-14 16:12:24 +03:00
|
|
|
|
await Utils.SendFeedback(
|
|
|
|
|
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)),
|
|
|
|
|
guildId, 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-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-05-14 16:12:24 +03:00
|
|
|
|
string.Format(config["WelcomeMessage"], user.Mention, guild.Name));
|
|
|
|
|
|
2022-07-15 19:23:45 +03:00
|
|
|
|
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;
|
|
|
|
|
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} ";
|
|
|
|
|
|
2022-06-06 18:39:47 +03:00
|
|
|
|
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,
|
2022-06-06 18:39:47 +03:00
|
|
|
|
Utils.Wrap(scheduledEvent.Name), location,
|
2022-05-14 16:12:24 +03:00
|
|
|
|
scheduledEvent.StartTime.ToUnixTimeSeconds().ToString(), Utils.Wrap(scheduledEvent.Description)),
|
|
|
|
|
true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task ScheduledEventCancelledEvent(SocketGuildEvent scheduledEvent) {
|
|
|
|
|
var guild = scheduledEvent.Guild;
|
|
|
|
|
var eventConfig = Boyfriend.GetGuildConfig(guild.Id);
|
|
|
|
|
var channel = guild.GetTextChannel(Convert.ToUInt64(eventConfig["EventCancelledChannel"]));
|
|
|
|
|
if (channel != 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);
|
|
|
|
|
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"))
|
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;
|
|
|
|
|
var eventConfig = Boyfriend.GetGuildConfig(guild.Id);
|
|
|
|
|
var channel = guild.GetTextChannel(Convert.ToUInt64(eventConfig["EventCompletedChannel"]));
|
|
|
|
|
if (channel != null)
|
2022-06-06 18:39:47 +03:00
|
|
|
|
await channel.SendMessageAsync(string.Format(Messages.EventCompleted, Utils.Wrap(scheduledEvent.Name),
|
|
|
|
|
Utils.Wrap(scheduledEvent.StartTime.Subtract(DateTimeOffset.Now).Negate().ToString())));
|
2021-12-07 21:27:27 +03:00
|
|
|
|
}
|
2022-08-30 18:15:01 +03:00
|
|
|
|
}
|