mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-01-31 00:19:00 +03:00
tired
This commit is contained in:
parent
3fa19a4794
commit
8644f70b14
7 changed files with 158 additions and 37 deletions
|
@ -2,21 +2,24 @@
|
|||
using Discord.WebSocket;
|
||||
|
||||
namespace Boyfriend;
|
||||
public class Boyfriend {
|
||||
public static class Boyfriend {
|
||||
|
||||
public static void Main(string[] args)
|
||||
=> new Boyfriend().MainAsync().GetAwaiter().GetResult();
|
||||
public static void Main()
|
||||
=> Init().GetAwaiter().GetResult();
|
||||
|
||||
public static readonly DiscordSocketClient Client = new();
|
||||
private static readonly DiscordSocketConfig Config = new() {
|
||||
MessageCacheSize = 250
|
||||
};
|
||||
public static readonly DiscordSocketClient Client = new(Config);
|
||||
|
||||
private async Task MainAsync() {
|
||||
private static async Task Init() {
|
||||
Client.Log += Log;
|
||||
var token = File.ReadAllText("token.txt").Trim();
|
||||
var token = (await File.ReadAllTextAsync("token.txt")).Trim();
|
||||
|
||||
await Client.LoginAsync(TokenType.Bot, token);
|
||||
await Client.StartAsync();
|
||||
|
||||
await new CommandHandler().InstallCommandsAsync();
|
||||
await new EventHandler().InitEvents();
|
||||
|
||||
await Task.Delay(-1);
|
||||
}
|
||||
|
|
|
@ -1,29 +0,0 @@
|
|||
using System.Reflection;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
|
||||
namespace Boyfriend;
|
||||
|
||||
public class CommandHandler {
|
||||
private readonly DiscordSocketClient _client = Boyfriend.Client;
|
||||
private readonly CommandService _commands = new CommandService();
|
||||
|
||||
public async Task InstallCommandsAsync() {
|
||||
_client.MessageReceived += HandleCommandAsync;
|
||||
await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), null);
|
||||
}
|
||||
|
||||
private async Task HandleCommandAsync(SocketMessage messageParam) {
|
||||
if (messageParam is not SocketUserMessage message) return;
|
||||
var argPos = 0;
|
||||
|
||||
if (!(message.HasCharPrefix('!', ref argPos) ||
|
||||
message.HasMentionPrefix(_client.CurrentUser, ref argPos)) ||
|
||||
message.Author.IsBot)
|
||||
return;
|
||||
|
||||
var context = new SocketCommandContext(_client, message);
|
||||
|
||||
await _commands.ExecuteAsync(context, argPos, null);
|
||||
}
|
||||
}
|
26
Boyfriend/Commands/BanModule.cs
Normal file
26
Boyfriend/Commands/BanModule.cs
Normal file
|
@ -0,0 +1,26 @@
|
|||
using Discord;
|
||||
using Discord.Commands;
|
||||
// ReSharper disable UnusedType.Global
|
||||
// ReSharper disable UnusedMember.Global
|
||||
|
||||
namespace Boyfriend.Commands;
|
||||
|
||||
public class BanModule : ModuleBase<SocketCommandContext> {
|
||||
|
||||
[Command("ban")]
|
||||
[Summary("Банит пользователя")]
|
||||
[Alias("бан")]
|
||||
public async Task Run(IUser toBan, TimeSpan duration, [Remainder]string reason)
|
||||
=> await BanUser(Context.Guild, Context.User, toBan, duration, reason);
|
||||
|
||||
public async void BanUser(IGuild guild, IUser author, IUser toBan, TimeSpan duration, string reason = "") {
|
||||
var authorMention = author.Mention;
|
||||
await toBan.SendMessageAsync("Тебя забанил " + authorMention + " за " + reason);
|
||||
await guild.AddBanAsync(toBan, 0, reason);
|
||||
await guild.GetSystemChannelAsync().Result.SendMessageAsync(authorMention + " банит " + toBan.Mention + " за "
|
||||
+ reason);
|
||||
var banTimer = new System.Timers.Timer(duration.Milliseconds);
|
||||
banTimer.Elapsed += UnbanModule.UnbanUser(guild, author, toBan, "Время наказания истекло").;
|
||||
banTimer.Start();
|
||||
}
|
||||
}
|
|
@ -1,4 +1,6 @@
|
|||
using Discord.Commands;
|
||||
// ReSharper disable UnusedType.Global
|
||||
// ReSharper disable UnusedMember.Global
|
||||
|
||||
namespace Boyfriend.Commands;
|
||||
|
||||
|
|
23
Boyfriend/Commands/Unban.cs
Normal file
23
Boyfriend/Commands/Unban.cs
Normal file
|
@ -0,0 +1,23 @@
|
|||
using Discord;
|
||||
using Discord.Commands;
|
||||
// ReSharper disable UnusedType.Global
|
||||
// ReSharper disable UnusedMember.Global
|
||||
|
||||
namespace Boyfriend.Commands;
|
||||
|
||||
public class UnbanModule : ModuleBase<SocketCommandContext> {
|
||||
|
||||
[Command("unban")]
|
||||
[Summary("Возвращает пользователя из бана")]
|
||||
[Alias("разбан")]
|
||||
public async Task Run(IUser toBan, TimeSpan duration, [Remainder]string reason)
|
||||
=> await UnbanUser(Context.Guild, Context.User, toBan, reason);
|
||||
|
||||
public async Task UnbanUser(IGuild guild, IUser author, IUser toBan, string reason = "") {
|
||||
var authorMention = author.Mention;
|
||||
await toBan.SendMessageAsync("Тебя разбанил " + authorMention + " за " + reason);
|
||||
await guild.RemoveBanAsync(toBan);
|
||||
await guild.GetSystemChannelAsync().Result.SendMessageAsync(authorMention + " возвращает из бана "
|
||||
+ toBan.Mention + " за " + reason);
|
||||
}
|
||||
}
|
79
Boyfriend/EventHandler.cs
Normal file
79
Boyfriend/EventHandler.cs
Normal file
|
@ -0,0 +1,79 @@
|
|||
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;
|
||||
private readonly CommandService _commands = new();
|
||||
|
||||
public async Task InitEvents() {
|
||||
_client.Ready += ReadyEvent;
|
||||
_client.MessageDeleted += MessageDeletedEvent;
|
||||
_client.MessageReceived += MessageReceivedEvent;
|
||||
_client.MessageUpdated += MessageUpdatedEvent;
|
||||
_client.UserJoined += UserJoinedEvent;
|
||||
await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), null);
|
||||
}
|
||||
|
||||
[Obsolete("Stop hard-coding things!")]
|
||||
private async Task ReadyEvent() {
|
||||
if (_client.GetChannel(618044439939645444) is not IMessageChannel botLogChannel)
|
||||
throw new ArgumentException("Invalid bot log channel");
|
||||
await botLogChannel.SendMessageAsync(Utils.GetBeep() +
|
||||
"Я запустился! (C#)");
|
||||
}
|
||||
|
||||
private static async Task MessageDeletedEvent(Cacheable<IMessage, ulong> message, ISocketMessageChannel channel) {
|
||||
var msg = message.Value;
|
||||
string toSend;
|
||||
if (msg == null)
|
||||
toSend = "Удалено сообщение в канале " + Utils.MentionChannel(channel.Id) + ", но я забыл что там было";
|
||||
else
|
||||
toSend = "Удалено сообщение от " + msg.Author.Mention + " в канале " + Utils.MentionChannel(channel.Id)
|
||||
+ ": " + Utils.Wrap(msg.Content);
|
||||
await Utils.GetAdminLogChannel().SendMessageAsync(toSend);
|
||||
}
|
||||
|
||||
private async Task MessageReceivedEvent(SocketMessage messageParam) {
|
||||
if (messageParam is not SocketUserMessage {Author: IGuildUser user} message) return;
|
||||
var argPos = 0;
|
||||
var guild = user.Guild;
|
||||
|
||||
if ((message.MentionedUsers.Count > 3 || message.MentionedRoles.Count > 2)
|
||||
&& !user.GuildPermissions.MentionEveryone)
|
||||
await new BanModule().BanUser(guild, guild.GetCurrentUserAsync().Result, user, TimeSpan.Zero,
|
||||
"Более 3-ёх упоминаний в одном сообщении");
|
||||
|
||||
if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos)) ||
|
||||
message.Author.IsBot)
|
||||
return;
|
||||
|
||||
var context = new SocketCommandContext(_client, message);
|
||||
|
||||
await _commands.ExecuteAsync(context, argPos, null);
|
||||
}
|
||||
|
||||
private static async Task MessageUpdatedEvent(Cacheable<IMessage, ulong> messageCached, SocketMessage messageSocket,
|
||||
ISocketMessageChannel channel) {
|
||||
var msg = messageCached.Value;
|
||||
string toSend;
|
||||
if (msg == null)
|
||||
toSend = "Отредактировано сообщение в канале "
|
||||
+ Utils.MentionChannel(channel.Id) + ", но я забыл что там было до редактирования: "
|
||||
+ Utils.Wrap(messageSocket.Content);
|
||||
else
|
||||
toSend = "Отредактировано сообщение от " + msg.Author.Mention + " в канале "
|
||||
+ Utils.MentionChannel(channel.Id) + ": " + Utils.Wrap(msg.Content)
|
||||
+ Utils.Wrap(messageSocket.Content);
|
||||
await Utils.GetAdminLogChannel().SendMessageAsync(toSend);
|
||||
}
|
||||
|
||||
private static async Task UserJoinedEvent(SocketGuildUser user) {
|
||||
await user.Guild.SystemChannel.SendMessageAsync(user.Mention + ", добро пожаловать на сервер "
|
||||
+ user.Guild.Name);
|
||||
}
|
||||
}
|
|
@ -1,8 +1,25 @@
|
|||
namespace Boyfriend;
|
||||
using Discord;
|
||||
|
||||
namespace Boyfriend;
|
||||
|
||||
public static class Utils {
|
||||
public static string GetBeep() {
|
||||
var letters = new[] { "а", "о", "и"};
|
||||
return "Б" + letters[new Random().Next(3)] + "п! ";
|
||||
}
|
||||
|
||||
[Obsolete("Stop hard-coding things!")]
|
||||
public static IMessageChannel GetAdminLogChannel() {
|
||||
if (Boyfriend.Client.GetChannel(870929165141032971) is not IMessageChannel adminLogChannel)
|
||||
throw new ArgumentException("Invalid admin log channel");
|
||||
return adminLogChannel;
|
||||
}
|
||||
|
||||
public static string Wrap(string original) {
|
||||
return original.Trim().Equals("") ? "" : "```" + original.Replace("```", "```") + "```";
|
||||
}
|
||||
|
||||
public static string MentionChannel(ulong id) {
|
||||
return "<#" + id + ">";
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue