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-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
|
|
|
|
|
|
|
|
|
foreach (var guild in Boyfriend.Client.Guilds) {
|
|
|
|
|
var channel = guild.GetTextChannel(Boyfriend.GetGuildConfig(guild).BotLogChannel);
|
|
|
|
|
if (channel == null) continue;
|
|
|
|
|
await channel.SendMessageAsync($"{Utils.GetBeep()}Я запустился! (C#)");
|
|
|
|
|
}
|
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
|
2021-12-15 22:07:04 +03:00
|
|
|
|
? $"Удалено сообщение в канале {Utils.MentionChannel(channel.Id)}, но я забыл что там было"
|
2021-12-10 14:25:29 +03:00
|
|
|
|
: $"Удалено сообщение от {msg.Author.Mention} в канале " +
|
|
|
|
|
$"{Utils.MentionChannel(channel.Id)}: {Environment.NewLine}{Utils.Wrap(msg.Content)}";
|
2021-12-15 22:07:04 +03:00
|
|
|
|
try {
|
|
|
|
|
await Utils.SilentSendAsync(await Utils.GetAdminLogChannel(
|
|
|
|
|
Boyfriend.FindGuild(channel.Value as ITextChannel)), toSend);
|
|
|
|
|
} catch (ArgumentException) {}
|
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-15 22:07:04 +03:00
|
|
|
|
BanModule.BanUser(guild, await guild.GetCurrentUserAsync(), 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;
|
2021-12-15 22:07:04 +03:00
|
|
|
|
if (message.Channel is not ITextChannel channel) throw new Exception();
|
2021-12-15 09:19:14 +03:00
|
|
|
|
if (!(message.HasStringPrefix(Boyfriend.GetGuildConfig(guild).Prefix, ref argPos)
|
|
|
|
|
|| message.HasMentionPrefix(Boyfriend.Client.CurrentUser, ref argPos))
|
2021-12-15 22:07:04 +03:00
|
|
|
|
|| user == await Boyfriend.FindGuild(channel).GetCurrentUserAsync()
|
2021-12-15 09:19:14 +03:00
|
|
|
|
|| 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)}";
|
2021-12-15 22:07:04 +03:00
|
|
|
|
try {
|
|
|
|
|
await Utils.SilentSendAsync(await Utils.GetAdminLogChannel(Boyfriend.FindGuild(channel as ITextChannel)),
|
|
|
|
|
toSend);
|
|
|
|
|
} catch (ArgumentException) {}
|
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
|
|
|
|
}
|
|
|
|
|
}
|