This repository has been archived on 2024-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
OctobotStealth/Boyfriend/EventHandler.cs

110 lines
4.5 KiB
C#
Raw Normal View History

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;
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
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) {
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
2021-12-10 14:25:29 +03:00
var toSend = msg == null
? string.Format(Messages.UncachedMessageDeleted, Utils.MentionChannel(channel.Id))
: 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)}";
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;
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
2021-12-07 21:27:27 +03:00
if ((message.MentionedUsers.Count > 3 || message.MentionedRoles.Count > 2)
&& !user.GuildPermissions.MentionEveryone)
await BanCommand.BanUser(guild, null, await guild.GetCurrentUserAsync(), user,
TimeSpan.FromMilliseconds(-1), Messages.AutobanReason);
2021-12-07 21:27:27 +03:00
try {
prev = prevsArray[1].Content;
prevFailsafe = prevsArray[2].Content;
}
catch (IndexOutOfRangeException) { }
if (!(message.HasStringPrefix(guildConfig.Prefix, ref argPos)
2021-12-15 09:19:14 +03:00
|| message.HasMentionPrefix(Boyfriend.Client.CurrentUser, ref argPos))
|| user == await guild.GetCurrentUserAsync()
2022-01-30 11:43:15 +03:00
|| user.IsBot && (message.Content.Contains(prev) || message.Content.Contains(prevFailsafe)))
2021-12-07 21:27:27 +03:00
return;
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
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
? string.Format(Messages.UncachedMessageEdited, messageSocket.Author.Mention,
Utils.MentionChannel(channel.Id)) +
2021-12-15 09:19:14 +03:00
Utils.Wrap(messageSocket.Content)
: string.Format(Messages.CachedMessageEdited, msg.Author.Mention, Utils.MentionChannel(channel.Id), nl, nl,
Utils.Wrap(msg.Content), nl, nl, Utils.Wrap(messageSocket.Content));
await Utils.SilentSendAsync(await Utils.GetAdminLogChannel(Boyfriend.FindGuild(channel)),
2021-12-15 22:07:04 +03:00
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;
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))
await Utils.SilentSendAsync(guild.SystemChannel, string.Format(config.WelcomeMessage!, user.Mention,
guild.Name));
2022-02-02 16:14:26 +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-01-30 11:43:15 +03:00
}