2022-01-18 20:38:15 +03:00
|
|
|
|
using System.Globalization;
|
2021-12-07 21:27:27 +03:00
|
|
|
|
using Boyfriend.Commands;
|
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
|
|
|
|
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;
|
2021-12-10 14:25:29 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 22:07:04 +03:00
|
|
|
|
private static async Task ReadyEvent() {
|
2021-12-15 09:19:14 +03:00
|
|
|
|
await Boyfriend.SetupGuildConfigs();
|
2021-12-15 22:07:04 +03:00
|
|
|
|
|
2022-01-18 20:38:15 +03:00
|
|
|
|
var i = new 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-01-18 20:38:15 +03:00
|
|
|
|
var config = Boyfriend.GetGuildConfig(guild);
|
2022-01-30 11:43:15 +03:00
|
|
|
|
var channel = guild.GetTextChannel(config.BotLogChannel.GetValueOrDefault(0));
|
2022-02-02 16:14:26 +03:00
|
|
|
|
Messages.Culture = new CultureInfo(config.Lang!);
|
|
|
|
|
|
2022-01-30 11:43:15 +03:00
|
|
|
|
if (!config.ReceiveStartupMessages.GetValueOrDefault(true) || channel == null) continue;
|
|
|
|
|
await channel.SendMessageAsync(string.Format(Messages.Ready, Utils.GetBeep(config.Lang!, 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-02-02 16:14:26 +03:00
|
|
|
|
|
2022-02-21 20:08:55 +03:00
|
|
|
|
var toSend = msg == null ? string.Format(Messages.UncachedMessageDeleted, Utils.MentionChannel(channel.Id))
|
2022-01-18 20:38:15 +03:00
|
|
|
|
: string.Format(Messages.CachedMessageDeleted, msg.Author.Mention) +
|
2021-12-10 14:25:29 +03:00
|
|
|
|
$"{Utils.MentionChannel(channel.Id)}: {Environment.NewLine}{Utils.Wrap(msg.Content)}";
|
2022-02-21 20:08:55 +03:00
|
|
|
|
await Utils.SilentSendAsync(await Utils.GetAdminLogChannel(Boyfriend.FindGuild(channel.Value)), toSend);
|
2021-12-07 21:27:27 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 09:19:14 +03:00
|
|
|
|
private static async Task MessageReceivedEvent(SocketMessage messageParam) {
|
|
|
|
|
if (messageParam is not SocketUserMessage message) return;
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
|
|
|
|
var argPos = 0;
|
2021-12-15 09:19:14 +03:00
|
|
|
|
var user = (IGuildUser) message.Author;
|
2021-12-07 21:27:27 +03:00
|
|
|
|
var guild = user.Guild;
|
2022-01-18 20:38:15 +03:00
|
|
|
|
var guildConfig = Boyfriend.GetGuildConfig(guild);
|
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-01-30 11:43:15 +03:00
|
|
|
|
Messages.Culture = new CultureInfo(guildConfig.Lang!);
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2022-02-21 20:08:55 +03:00
|
|
|
|
if ((message.MentionedUsers.Count > 3 || message.MentionedRoles.Count > 2) &&
|
|
|
|
|
!user.GuildPermissions.MentionEveryone)
|
2022-01-18 20:38:15 +03:00
|
|
|
|
await BanCommand.BanUser(guild, null, await guild.GetCurrentUserAsync(), user,
|
|
|
|
|
TimeSpan.FromMilliseconds(-1), Messages.AutobanReason);
|
2021-12-07 21:27:27 +03:00
|
|
|
|
|
2022-01-18 20:38:15 +03:00
|
|
|
|
try {
|
|
|
|
|
prev = prevsArray[1].Content;
|
|
|
|
|
prevFailsafe = prevsArray[2].Content;
|
2022-02-21 20:08:55 +03:00
|
|
|
|
} catch (IndexOutOfRangeException) {}
|
2022-01-18 20:38:15 +03:00
|
|
|
|
|
2022-02-21 20:08:55 +03:00
|
|
|
|
if (!(message.HasStringPrefix(guildConfig.Prefix, ref argPos) ||
|
|
|
|
|
message.HasMentionPrefix(Boyfriend.Client.CurrentUser, ref argPos)) ||
|
|
|
|
|
user == await guild.GetCurrentUserAsync() ||
|
|
|
|
|
user.IsBot && (message.Content.Contains(prev) || message.Content.Contains(prevFailsafe)))
|
2021-12-07 21:27:27 +03:00
|
|
|
|
return;
|
|
|
|
|
|
2022-01-18 20:38:15 +03:00
|
|
|
|
await CommandHandler.HandleCommand(message);
|
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;
|
2021-12-10 14:25:29 +03:00
|
|
|
|
var nl = Environment.NewLine;
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2022-01-18 20:38:15 +03:00
|
|
|
|
if (msg != null && msg.Content == messageSocket.Content) return;
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2021-12-10 14:25:29 +03:00
|
|
|
|
var toSend = msg == null
|
2022-01-18 20:38:15 +03:00
|
|
|
|
? string.Format(Messages.UncachedMessageEdited, messageSocket.Author.Mention,
|
2022-02-21 20:08:55 +03:00
|
|
|
|
Utils.MentionChannel(channel.Id)) + Utils.Wrap(messageSocket.Content) : string.Format(
|
|
|
|
|
Messages.CachedMessageEdited, msg.Author.Mention, Utils.MentionChannel(channel.Id), nl, nl,
|
2022-01-18 20:38:15 +03:00
|
|
|
|
Utils.Wrap(msg.Content), nl, nl, Utils.Wrap(messageSocket.Content));
|
2022-02-21 20:08:55 +03:00
|
|
|
|
await Utils.SilentSendAsync(await Utils.GetAdminLogChannel(Boyfriend.FindGuild(channel)), toSend);
|
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-01-18 20:38:15 +03:00
|
|
|
|
var config = Boyfriend.GetGuildConfig(guild);
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2022-01-30 11:43:15 +03:00
|
|
|
|
if (config.SendWelcomeMessages.GetValueOrDefault(true))
|
2022-02-21 20:08:55 +03:00
|
|
|
|
await Utils.SilentSendAsync(guild.SystemChannel,
|
|
|
|
|
string.Format(config.WelcomeMessage!, user.Mention, guild.Name));
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2022-01-18 20:38:15 +03:00
|
|
|
|
if (config.DefaultRole != 0)
|
2022-01-30 11:43:15 +03:00
|
|
|
|
await user.AddRoleAsync(Utils.ParseRole(guild, config.DefaultRole.ToString()!));
|
2021-12-07 21:27:27 +03:00
|
|
|
|
}
|
2022-02-21 20:08:55 +03:00
|
|
|
|
}
|