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

83 lines
4 KiB
C#
Raw Normal View History

2021-12-07 21:27:27 +03:00
using System.Reflection;
using Boyfriend.Commands;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
namespace Boyfriend;
public class EventHandler {
private readonly DiscordSocketClient _client = Boyfriend.Client;
2021-12-15 09:19:14 +03:00
public static readonly CommandService Commands = new();
2021-12-07 21:27:27 +03:00
public async Task InitEvents() {
_client.Ready += ReadyEvent;
_client.MessageDeleted += MessageDeletedEvent;
_client.MessageReceived += MessageReceivedEvent;
_client.MessageUpdated += MessageUpdatedEvent;
_client.UserJoined += UserJoinedEvent;
2021-12-15 09:19:14 +03:00
await Commands.AddModulesAsync(Assembly.GetEntryAssembly(), null);
2021-12-10 14:25:29 +03:00
}
2021-12-07 21:27:27 +03:00
[Obsolete("Stop hard-coding things!")]
private async Task ReadyEvent() {
if (_client.GetChannel(618044439939645444) is not IMessageChannel botLogChannel)
2021-12-15 09:19:14 +03:00
throw new Exception("Invalid bot log channel");
2021-12-10 14:25:29 +03:00
await botLogChannel.SendMessageAsync($"{Utils.GetBeep()}Я запустился! (C#)");
2021-12-15 09:19:14 +03:00
await Boyfriend.SetupGuildConfigs();
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;
2021-12-10 14:25:29 +03:00
var toSend = msg == null
? "Удалено сообщение в канале {Utils.MentionChannel(channel.Id)}, но я забыл что там было"
: $"Удалено сообщение от {msg.Author.Mention} в канале " +
$"{Utils.MentionChannel(channel.Id)}: {Environment.NewLine}{Utils.Wrap(msg.Content)}";
await Utils.SilentSendAsync(Utils.GetAdminLogChannel(), 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;
var user = (IGuildUser) message.Author;
2021-12-07 21:27:27 +03:00
var guild = user.Guild;
2021-12-15 09:19:14 +03:00
var argPos = 0;
2021-12-07 21:27:27 +03:00
if ((message.MentionedUsers.Count > 3 || message.MentionedRoles.Count > 2)
&& !user.GuildPermissions.MentionEveryone)
2021-12-10 14:25:29 +03:00
BanModule.BanUser(guild, guild.GetCurrentUserAsync().Result, user, TimeSpan.FromMilliseconds(-1),
2021-12-07 21:27:27 +03:00
"Более 3-ёх упоминаний в одном сообщении");
2021-12-15 09:19:14 +03:00
var prevs = await message.Channel.GetMessagesAsync(3).FlattenAsync();
var prevsArray = prevs as IMessage[] ?? prevs.ToArray();
var prev = prevsArray[1].Content;
var prevFailsafe = prevsArray[2].Content;
if (!(message.HasStringPrefix(Boyfriend.GetGuildConfig(guild).Prefix, ref argPos)
|| message.HasMentionPrefix(Boyfriend.Client.CurrentUser, ref argPos))
|| user.IsBot && message.Content.Contains(prev) || message.Content.Contains(prevFailsafe))
2021-12-07 21:27:27 +03:00
return;
2021-12-15 09:19:14 +03:00
await CommandHandler.HandleCommand(message, argPos);
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;
2021-12-15 09:19:14 +03:00
if (msg.Content == messageSocket.Content) return;
2021-12-10 14:25:29 +03:00
var toSend = msg == null
? $"Отредактировано сообщение от {messageSocket.Author.Mention} в канале" +
2021-12-15 09:19:14 +03:00
$" {Utils.MentionChannel(channel.Id)}," + " но я забыл что там было до редактирования: " +
Utils.Wrap(messageSocket.Content)
2021-12-10 14:25:29 +03:00
: $"Отредактировано сообщение от {msg.Author.Mention} " +
$"в канале {Utils.MentionChannel(channel.Id)}." +
$"{nl}До:{nl}{Utils.Wrap(msg.Content)}{nl}После:{nl}{Utils.Wrap(messageSocket.Content)}";
await Utils.SilentSendAsync(Utils.GetAdminLogChannel(), 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;
await guild.SystemChannel.SendMessageAsync($"{user.Mention}, добро пожаловать на сервер {guild.Name}");
2021-12-07 21:27:27 +03:00
}
}