forked from TeamInklings/Octobot
Seal all possible classes, add LICENSE, follow async naming conventions
This commit is contained in:
parent
ac63719a0b
commit
e767205c1a
20 changed files with 873 additions and 123 deletions
|
@ -20,7 +20,7 @@ public static class Boyfriend {
|
|||
};
|
||||
|
||||
public static readonly DiscordSocketClient Client = new(Config);
|
||||
private static readonly Game Activity = new("Toby Fox - The World Revolving", ActivityType.Listening);
|
||||
private static readonly Game Activity = new("UNDEAD CORPORATION - Everything will freeze", ActivityType.Listening);
|
||||
|
||||
private static readonly Dictionary<ulong, Dictionary<string, string>> GuildConfigDictionary = new();
|
||||
|
||||
|
@ -71,7 +71,7 @@ public static class Boyfriend {
|
|||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public static async Task WriteGuildConfig(ulong id) {
|
||||
public static async Task WriteGuildConfigAsync(ulong id) {
|
||||
var json = JsonConvert.SerializeObject(GuildConfigDictionary[id], Formatting.Indented);
|
||||
var removedRoles = JsonConvert.SerializeObject(RemovedRolesDictionary[id], Formatting.Indented);
|
||||
|
||||
|
|
|
@ -8,10 +8,19 @@
|
|||
<LangVersion>default</LangVersion>
|
||||
<Title>Boyfriend</Title>
|
||||
<Authors>l1ttle</Authors>
|
||||
<PackageProjectUrl>https://github.com/l1ttleO/Boyfriend-CSharp</PackageProjectUrl>
|
||||
<RepositoryUrl>https://github.com/l1ttleO/Boyfriend-CSharp</RepositoryUrl>
|
||||
<PackageProjectUrl>https://git.cavej376.xyz/Octol1ttle/Boyfriend-CSharp</PackageProjectUrl>
|
||||
<RepositoryUrl>https://git.cavej376.xyz/Octol1ttle/Boyfriend-CSharp</RepositoryUrl>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<PackageVersion>1.0.1</PackageVersion>
|
||||
<PackageLicenseUrl>https://git.cavej376.xyz/Octol1ttle/Boyfriend-CSharp/src/branch/master/LICENSE</PackageLicenseUrl>
|
||||
<NeutralLanguage>en</NeutralLanguage>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||
<DefineConstants/>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<DebugType>none</DebugType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
@ -7,16 +7,17 @@ using Discord.WebSocket;
|
|||
|
||||
namespace Boyfriend;
|
||||
|
||||
public class CommandProcessor {
|
||||
public sealed class CommandProcessor {
|
||||
private const string Success = ":white_check_mark: ";
|
||||
private const string MissingArgument = ":keyboard: ";
|
||||
private const string InvalidArgument = ":construction: ";
|
||||
private const string NoAccess = ":no_entry_sign: ";
|
||||
private const string CantInteract = ":vertical_traffic_light: ";
|
||||
|
||||
public static readonly Command[] Commands = {
|
||||
public static readonly ICommand[] Commands = {
|
||||
new BanCommand(), new ClearCommand(), new HelpCommand(),
|
||||
new KickCommand(), new MuteCommand(), new PingCommand(),
|
||||
new SelfBanCommand(),
|
||||
new SettingsCommand(), new UnbanCommand(), new UnmuteCommand()
|
||||
};
|
||||
|
||||
|
@ -35,14 +36,12 @@ public class CommandProcessor {
|
|||
Context = new SocketCommandContext(Boyfriend.Client, message);
|
||||
}
|
||||
|
||||
public async Task HandleCommand() {
|
||||
_stackedReplyMessage.Clear();
|
||||
_stackedPrivateFeedback.Clear();
|
||||
_stackedPublicFeedback.Clear();
|
||||
public async Task HandleCommandAsync() {
|
||||
var guild = Context.Guild;
|
||||
var config = Boyfriend.GetGuildConfig(guild.Id);
|
||||
var muteRole = Utils.GetMuteRole(guild);
|
||||
|
||||
if (GetMember().Roles.Contains(Utils.GetMuteRole(guild))) {
|
||||
if (GetMember().Roles.Contains(muteRole)) {
|
||||
await Context.Message.ReplyAsync(Messages.UserCannotUnmuteThemselves);
|
||||
return;
|
||||
}
|
||||
|
@ -54,35 +53,36 @@ public class CommandProcessor {
|
|||
}
|
||||
|
||||
var list = Context.Message.Content.Split("\n");
|
||||
foreach (var line in list) {
|
||||
RunCommandOnLine(line, regex);
|
||||
var cleanList = Context.Message.CleanContent.Split("\n");
|
||||
for (var i = 0; i < list.Length; i++) {
|
||||
RunCommandOnLine(list[i], cleanList[i], regex);
|
||||
if (_stackedReplyMessage.Length > 0) _ = Context.Channel.TriggerTypingAsync();
|
||||
var member = Boyfriend.Client.GetGuild(Context.Guild.Id)
|
||||
.GetUser(Context.User.Id); // Getting an up-to-date copy
|
||||
if (member == null || member.Roles.Contains(muteRole)
|
||||
|| member.TimedOutUntil.GetValueOrDefault(DateTimeOffset.UnixEpoch).ToUnixTimeSeconds() >
|
||||
DateTimeOffset.Now.ToUnixTimeSeconds())
|
||||
break;
|
||||
}
|
||||
|
||||
await Task.WhenAll(_tasks);
|
||||
_tasks.Clear();
|
||||
|
||||
if (ConfigWriteScheduled) await Boyfriend.WriteGuildConfig(guild.Id);
|
||||
if (ConfigWriteScheduled) await Boyfriend.WriteGuildConfigAsync(guild.Id);
|
||||
|
||||
if (_stackedReplyMessage.Length > 0) _ = Context.Message.ReplyAsync(_stackedReplyMessage.ToString());
|
||||
|
||||
var adminChannel = Utils.GetAdminLogChannel(guild.Id);
|
||||
var systemChannel = guild.SystemChannel;
|
||||
if (_stackedPrivateFeedback.Length > 0 && adminChannel != null && adminChannel.Id != Context.Message.Channel.Id)
|
||||
_ = Utils.SilentSendAsync(adminChannel, _stackedPrivateFeedback.ToString());
|
||||
if (_stackedPublicFeedback.Length > 0 && systemChannel != null && systemChannel.Id != adminChannel?.Id
|
||||
&& systemChannel.Id != Context.Message.Channel.Id)
|
||||
_ = Utils.SilentSendAsync(systemChannel, _stackedPublicFeedback.ToString());
|
||||
SendFeedbacks();
|
||||
}
|
||||
|
||||
private void RunCommandOnLine(string line, Regex regex) {
|
||||
private void RunCommandOnLine(string line, string cleanLine, Regex regex) {
|
||||
foreach (var command in Commands) {
|
||||
var lineNoMention = regex.Replace(MentionRegex.Replace(line, "", 1), "", 1);
|
||||
if (lineNoMention == line
|
||||
|| !command.Aliases.Contains(regex.Replace(lineNoMention, "", 1).Trim().ToLower().Split()[0]))
|
||||
|| !command.Aliases.Contains(lineNoMention.Trim().ToLower().Split()[0]))
|
||||
continue;
|
||||
|
||||
var args = line.Split().Skip(1).ToArray();
|
||||
_tasks.Add(command.Run(this, args));
|
||||
var args = line.Split().Skip(lineNoMention.StartsWith(" ") ? 2 : 1).ToArray();
|
||||
var cleanArgs = cleanLine.Split().Skip(lineNoMention.StartsWith(" ") ? 2 : 1).ToArray();
|
||||
_tasks.Add(command.RunAsync(this, args, cleanArgs));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -94,6 +94,26 @@ public class CommandProcessor {
|
|||
var format = string.Format(Messages.FeedbackFormat, Context.User.Mention, action);
|
||||
if (isPublic) Utils.SafeAppendToBuilder(_stackedPublicFeedback, format, Context.Guild.SystemChannel);
|
||||
Utils.SafeAppendToBuilder(_stackedPrivateFeedback, format, Utils.GetAdminLogChannel(Context.Guild.Id));
|
||||
if (_tasks.Count == 0) SendFeedbacks(false);
|
||||
}
|
||||
|
||||
private void SendFeedbacks(bool reply = true) {
|
||||
if (reply && _stackedReplyMessage.Length > 0)
|
||||
_ = Context.Message.ReplyAsync(_stackedReplyMessage.ToString(), false, null, AllowedMentions.None);
|
||||
|
||||
var adminChannel = Utils.GetAdminLogChannel(Context.Guild.Id);
|
||||
var systemChannel = Context.Guild.SystemChannel;
|
||||
if (_stackedPrivateFeedback.Length > 0 && adminChannel != null &&
|
||||
adminChannel.Id != Context.Message.Channel.Id) {
|
||||
_ = Utils.SilentSendAsync(adminChannel, _stackedPrivateFeedback.ToString());
|
||||
_stackedPrivateFeedback.Clear();
|
||||
}
|
||||
|
||||
if (_stackedPublicFeedback.Length > 0 && systemChannel != null && systemChannel.Id != adminChannel?.Id
|
||||
&& systemChannel.Id != Context.Message.Channel.Id) {
|
||||
_ = Utils.SilentSendAsync(systemChannel, _stackedPublicFeedback.ToString());
|
||||
_stackedPublicFeedback.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
public string? GetRemaining(string[] from, int startIndex, string? argument) {
|
||||
|
@ -105,7 +125,7 @@ public class CommandProcessor {
|
|||
return null;
|
||||
}
|
||||
|
||||
public SocketUser? GetUser(string[] args, int index, string? argument) {
|
||||
public SocketUser? GetUser(string[] args, string[] cleanArgs, int index, string? argument) {
|
||||
if (index >= args.Length) {
|
||||
Utils.SafeAppendToBuilder(_stackedReplyMessage, $"{MissingArgument}{Messages.MissingUser}",
|
||||
Context.Message);
|
||||
|
@ -115,7 +135,8 @@ public class CommandProcessor {
|
|||
var user = Utils.ParseUser(args[index]);
|
||||
if (user == null && argument != null)
|
||||
Utils.SafeAppendToBuilder(_stackedReplyMessage,
|
||||
$"{InvalidArgument}{string.Format(Messages.InvalidUser, args[index])}", Context.Message);
|
||||
$"{InvalidArgument}{string.Format(Messages.InvalidUser, Utils.Wrap(cleanArgs[index]))}",
|
||||
Context.Message);
|
||||
return user;
|
||||
}
|
||||
|
||||
|
@ -141,7 +162,7 @@ public class CommandProcessor {
|
|||
return member;
|
||||
}
|
||||
|
||||
public SocketGuildUser? GetMember(string[] args, int index, string? argument) {
|
||||
public SocketGuildUser? GetMember(string[] args, string[] cleanArgs, int index, string? argument) {
|
||||
if (index >= args.Length) {
|
||||
Utils.SafeAppendToBuilder(_stackedReplyMessage, $"{MissingArgument}{Messages.MissingMember}",
|
||||
Context.Message);
|
||||
|
@ -151,7 +172,8 @@ public class CommandProcessor {
|
|||
var member = Context.Guild.GetUser(Utils.ParseMention(args[index]));
|
||||
if (member == null && argument != null)
|
||||
Utils.SafeAppendToBuilder(_stackedReplyMessage,
|
||||
$"{InvalidArgument}{string.Format(Messages.InvalidMember, Utils.Wrap(args[index]))}", Context.Message);
|
||||
$"{InvalidArgument}{string.Format(Messages.InvalidMember, Utils.Wrap(cleanArgs[index]))}",
|
||||
Context.Message);
|
||||
return member;
|
||||
}
|
||||
|
||||
|
@ -167,9 +189,12 @@ public class CommandProcessor {
|
|||
}
|
||||
|
||||
var id = Utils.ParseMention(args[index]);
|
||||
if (Context.Guild.GetBanAsync(id) != null) return id;
|
||||
Utils.SafeAppendToBuilder(_stackedReplyMessage, Messages.UserNotBanned, Context.Message);
|
||||
return null;
|
||||
if (Context.Guild.GetBanAsync(id) == null) {
|
||||
Utils.SafeAppendToBuilder(_stackedReplyMessage, Messages.UserNotBanned, Context.Message);
|
||||
return null;
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
public int? GetNumberRange(string[] args, int index, int min, int max, string? argument) {
|
||||
|
|
|
@ -3,11 +3,11 @@ using Discord.WebSocket;
|
|||
|
||||
namespace Boyfriend.Commands;
|
||||
|
||||
public class BanCommand : Command {
|
||||
public override string[] Aliases { get; } = { "ban", "бан" };
|
||||
public sealed class BanCommand : ICommand {
|
||||
public string[] Aliases { get; } = { "ban", "бан" };
|
||||
|
||||
public override async Task Run(CommandProcessor cmd, string[] args) {
|
||||
var toBan = cmd.GetUser(args, 0, "ToBan");
|
||||
public async Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
|
||||
var toBan = cmd.GetUser(args, cleanArgs, 0, "ToBan");
|
||||
if (toBan == null || !cmd.HasPermission(GuildPermission.BanMembers)) return;
|
||||
|
||||
var memberToBan = cmd.GetMember(toBan, null);
|
||||
|
@ -34,11 +34,7 @@ public class BanCommand : Command {
|
|||
cmd.Reply(feedback, ":hammer: ");
|
||||
cmd.Audit(feedback);
|
||||
|
||||
if (duration.TotalSeconds > 0) {
|
||||
var _ = async () => {
|
||||
await Task.Delay(duration);
|
||||
await UnbanCommand.UnbanUser(cmd, toBan.Id, Messages.PunishmentExpired);
|
||||
};
|
||||
}
|
||||
if (duration.TotalSeconds > 0)
|
||||
await Task.FromResult(Utils.DelayedUnbanAsync(cmd, toBan.Id, Messages.PunishmentExpired, duration));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,15 +3,15 @@ using Discord.WebSocket;
|
|||
|
||||
namespace Boyfriend.Commands;
|
||||
|
||||
public class ClearCommand : Command {
|
||||
public override string[] Aliases { get; } = { "clear", "purge", "очистить", "стереть" };
|
||||
public sealed class ClearCommand : ICommand {
|
||||
public string[] Aliases { get; } = { "clear", "purge", "очистить", "стереть" };
|
||||
|
||||
public override async Task Run(CommandProcessor cmd, string[] args) {
|
||||
public async Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
|
||||
if (cmd.Context.Channel is not SocketTextChannel channel) throw new Exception();
|
||||
|
||||
if (!cmd.HasPermission(GuildPermission.ManageMessages)) return;
|
||||
|
||||
var toDelete = cmd.GetNumberRange(args, 0, 1, 200, "ClearAmount");
|
||||
var toDelete = cmd.GetNumberRange(cleanArgs, 0, 1, 200, "ClearAmount");
|
||||
if (toDelete == null) return;
|
||||
var messages = await channel.GetMessagesAsync((int)(toDelete + 1)).FlattenAsync();
|
||||
|
||||
|
@ -20,4 +20,4 @@ public class ClearCommand : Command {
|
|||
|
||||
cmd.Audit(string.Format(Messages.FeedbackMessagesCleared, (toDelete + 1).ToString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
namespace Boyfriend.Commands;
|
||||
|
||||
public abstract class Command {
|
||||
public abstract string[] Aliases { get; }
|
||||
|
||||
public abstract Task Run(CommandProcessor cmd, string[] args);
|
||||
}
|
|
@ -2,10 +2,10 @@
|
|||
|
||||
namespace Boyfriend.Commands;
|
||||
|
||||
public class HelpCommand : Command {
|
||||
public override string[] Aliases { get; } = { "help", "помощь", "справка" };
|
||||
public sealed class HelpCommand : ICommand {
|
||||
public string[] Aliases { get; } = { "help", "помощь", "справка" };
|
||||
|
||||
public override Task Run(CommandProcessor cmd, string[] args) {
|
||||
public Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
|
||||
var prefix = Boyfriend.GetGuildConfig(cmd.Context.Guild.Id)["Prefix"];
|
||||
var toSend = Boyfriend.StringBuilder.Append(Messages.CommandHelp);
|
||||
|
||||
|
@ -17,4 +17,4 @@ public class HelpCommand : Command {
|
|||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
7
Boyfriend/Commands/ICommand.cs
Normal file
7
Boyfriend/Commands/ICommand.cs
Normal file
|
@ -0,0 +1,7 @@
|
|||
namespace Boyfriend.Commands;
|
||||
|
||||
public interface ICommand {
|
||||
public string[] Aliases { get; }
|
||||
|
||||
public Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs);
|
||||
}
|
|
@ -3,19 +3,19 @@ using Discord.WebSocket;
|
|||
|
||||
namespace Boyfriend.Commands;
|
||||
|
||||
public class KickCommand : Command {
|
||||
public override string[] Aliases { get; } = { "kick", "кик", "выгнать" };
|
||||
public sealed class KickCommand : ICommand {
|
||||
public string[] Aliases { get; } = { "kick", "кик", "выгнать" };
|
||||
|
||||
public override async Task Run(CommandProcessor cmd, string[] args) {
|
||||
var toKick = cmd.GetMember(args, 0, "ToKick");
|
||||
public async Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
|
||||
var toKick = cmd.GetMember(args, cleanArgs, 0, "ToKick");
|
||||
if (toKick == null || !cmd.HasPermission(GuildPermission.KickMembers)) return;
|
||||
|
||||
if (!cmd.CanInteractWith(toKick, "Kick")) return;
|
||||
|
||||
await KickMember(cmd, toKick, cmd.GetRemaining(args, 1, "KickReason"));
|
||||
await KickMemberAsync(cmd, toKick, cmd.GetRemaining(args, 1, "KickReason"));
|
||||
}
|
||||
|
||||
private static async Task KickMember(CommandProcessor cmd, SocketGuildUser toKick, string? reason) {
|
||||
private static async Task KickMemberAsync(CommandProcessor cmd, SocketGuildUser toKick, string? reason) {
|
||||
if (reason == null) return;
|
||||
var guildKickMessage = $"({cmd.Context.User}) {reason}";
|
||||
|
||||
|
@ -28,4 +28,4 @@ public class KickCommand : Command {
|
|||
cmd.Reply(format, ":police_car: ");
|
||||
cmd.Audit(format);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,11 +4,11 @@ using Discord.WebSocket;
|
|||
|
||||
namespace Boyfriend.Commands;
|
||||
|
||||
public class MuteCommand : Command {
|
||||
public override string[] Aliases { get; } = { "mute", "timeout", "заглушить", "мут" };
|
||||
public sealed class MuteCommand : ICommand {
|
||||
public string[] Aliases { get; } = { "mute", "timeout", "заглушить", "мут" };
|
||||
|
||||
public override async Task Run(CommandProcessor cmd, string[] args) {
|
||||
var toMute = cmd.GetMember(args, 0, "ToMute");
|
||||
public async Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
|
||||
var toMute = cmd.GetMember(args, cleanArgs, 0, "ToMute");
|
||||
if (toMute == null) return;
|
||||
|
||||
var duration = CommandProcessor.GetTimeSpan(args, 1);
|
||||
|
@ -16,13 +16,12 @@ public class MuteCommand : Command {
|
|||
if (reason == null) return;
|
||||
var role = Utils.GetMuteRole(cmd.Context.Guild);
|
||||
|
||||
if (role != null) {
|
||||
if (toMute.Roles.Contains(role) || (toMute.TimedOutUntil != null &&
|
||||
toMute.TimedOutUntil.Value.ToUnixTimeMilliseconds() >
|
||||
DateTimeOffset.Now.ToUnixTimeMilliseconds())) {
|
||||
cmd.Reply(Messages.MemberAlreadyMuted, ":x: ");
|
||||
return;
|
||||
}
|
||||
if ((role != null && toMute.Roles.Contains(role))
|
||||
|| (toMute.TimedOutUntil != null
|
||||
&& toMute.TimedOutUntil.Value.ToUnixTimeSeconds()
|
||||
> DateTimeOffset.Now.ToUnixTimeSeconds())) {
|
||||
cmd.Reply(Messages.MemberAlreadyMuted, ":x: ");
|
||||
return;
|
||||
}
|
||||
|
||||
var rolesRemoved = Boyfriend.GetRemovedRoles(cmd.Context.Guild.Id);
|
||||
|
@ -36,10 +35,10 @@ public class MuteCommand : Command {
|
|||
|
||||
if (!cmd.HasPermission(GuildPermission.ModerateMembers) || !cmd.CanInteractWith(toMute, "Mute")) return;
|
||||
|
||||
await MuteMember(cmd, toMute, duration, reason);
|
||||
await MuteMemberAsync(cmd, toMute, duration, reason);
|
||||
}
|
||||
|
||||
private static async Task MuteMember(CommandProcessor cmd, SocketGuildUser toMute,
|
||||
private static async Task MuteMemberAsync(CommandProcessor cmd, SocketGuildUser toMute,
|
||||
TimeSpan duration, string reason) {
|
||||
var guild = cmd.Context.Guild;
|
||||
var config = Boyfriend.GetGuildConfig(guild.Id);
|
||||
|
@ -66,12 +65,8 @@ public class MuteCommand : Command {
|
|||
|
||||
await toMute.AddRoleAsync(role, requestOptions);
|
||||
|
||||
if (hasDuration) {
|
||||
var _ = async () => {
|
||||
await Task.Delay(duration);
|
||||
await UnmuteCommand.UnmuteMember(cmd, toMute, Messages.PunishmentExpired);
|
||||
};
|
||||
}
|
||||
if (hasDuration)
|
||||
await Task.FromResult(Utils.DelayedUnmuteAsync(cmd, toMute, Messages.PunishmentExpired, duration));
|
||||
} else {
|
||||
if (!hasDuration || duration.TotalDays > 28) {
|
||||
cmd.Reply(Messages.DurationRequiredForTimeOuts, ":x: ");
|
||||
|
@ -92,4 +87,4 @@ public class MuteCommand : Command {
|
|||
cmd.Reply(feedback, ":mute: ");
|
||||
cmd.Audit(feedback);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
namespace Boyfriend.Commands;
|
||||
|
||||
public class PingCommand : Command {
|
||||
public override string[] Aliases { get; } = { "ping", "latency", "pong", "пинг", "задержка", "понг" };
|
||||
public sealed class PingCommand : ICommand {
|
||||
public string[] Aliases { get; } = { "ping", "latency", "pong", "пинг", "задержка", "понг" };
|
||||
|
||||
public override Task Run(CommandProcessor cmd, string[] args) {
|
||||
public Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
|
||||
var builder = Boyfriend.StringBuilder;
|
||||
|
||||
builder.Append(Utils.GetBeep())
|
||||
|
@ -15,4 +15,4 @@ public class PingCommand : Command {
|
|||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
9
Boyfriend/Commands/SelfBanCommand.cs
Normal file
9
Boyfriend/Commands/SelfBanCommand.cs
Normal file
|
@ -0,0 +1,9 @@
|
|||
namespace Boyfriend.Commands;
|
||||
|
||||
public sealed class SelfBanCommand : ICommand {
|
||||
public string[] Aliases { get; } = { "grantoverseer", "grant", "overseer", "voooo", "overseergrant", "special" };
|
||||
|
||||
public async Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
|
||||
await BanCommand.BanUser(cmd, cmd.Context.User, TimeSpan.FromMilliseconds(-1), "");
|
||||
}
|
||||
}
|
|
@ -2,10 +2,10 @@
|
|||
|
||||
namespace Boyfriend.Commands;
|
||||
|
||||
public class SettingsCommand : Command {
|
||||
public override string[] Aliases { get; } = { "settings", "config", "настройки", "конфиг" };
|
||||
public sealed class SettingsCommand : ICommand {
|
||||
public string[] Aliases { get; } = { "settings", "config", "настройки", "конфиг" };
|
||||
|
||||
public override Task Run(CommandProcessor cmd, string[] args) {
|
||||
public Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
|
||||
if (!cmd.HasPermission(GuildPermission.ManageGuild)) return Task.CompletedTask;
|
||||
|
||||
var guild = cmd.Context.Guild;
|
||||
|
@ -48,7 +48,7 @@ public class SettingsCommand : Command {
|
|||
|
||||
var exists = false;
|
||||
// ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator
|
||||
// The performance impact is not worth it
|
||||
// Too many allocations
|
||||
foreach (var setting in Boyfriend.DefaultConfig.Keys) {
|
||||
if (selectedSetting != setting.ToLower()) continue;
|
||||
selectedSetting = setting;
|
||||
|
@ -161,4 +161,4 @@ public class SettingsCommand : Command {
|
|||
private static bool IsBool(string value) {
|
||||
return value is "true" or "false";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,10 +2,10 @@
|
|||
|
||||
namespace Boyfriend.Commands;
|
||||
|
||||
public class UnbanCommand : Command {
|
||||
public override string[] Aliases { get; } = { "unban", "разбан" };
|
||||
public sealed class UnbanCommand : ICommand {
|
||||
public string[] Aliases { get; } = { "unban", "разбан" };
|
||||
|
||||
public override async Task Run(CommandProcessor cmd, string[] args) {
|
||||
public async Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
|
||||
if (!cmd.HasPermission(GuildPermission.BanMembers)) return;
|
||||
|
||||
var id = cmd.GetBan(args, 0);
|
||||
|
@ -13,10 +13,10 @@ public class UnbanCommand : Command {
|
|||
var reason = cmd.GetRemaining(args, 1, "UnbanReason");
|
||||
if (reason == null) return;
|
||||
|
||||
await UnbanUser(cmd, id.Value, reason);
|
||||
await UnbanUserAsync(cmd, id.Value, reason);
|
||||
}
|
||||
|
||||
public static async Task UnbanUser(CommandProcessor cmd, ulong id, string reason) {
|
||||
public static async Task UnbanUserAsync(CommandProcessor cmd, ulong id, string reason) {
|
||||
var requestOptions = Utils.GetRequestOptions($"({cmd.Context.User}) {reason}");
|
||||
await cmd.Context.Guild.RemoveBanAsync(id, requestOptions);
|
||||
|
||||
|
@ -24,4 +24,4 @@ public class UnbanCommand : Command {
|
|||
cmd.Reply(feedback);
|
||||
cmd.Audit(feedback);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,19 +3,19 @@ using Discord.WebSocket;
|
|||
|
||||
namespace Boyfriend.Commands;
|
||||
|
||||
public class UnmuteCommand : Command {
|
||||
public override string[] Aliases { get; } = { "unmute", "размут" };
|
||||
public sealed class UnmuteCommand : ICommand {
|
||||
public string[] Aliases { get; } = { "unmute", "размут" };
|
||||
|
||||
public override async Task Run(CommandProcessor cmd, string[] args) {
|
||||
public async Task RunAsync(CommandProcessor cmd, string[] args, string[] cleanArgs) {
|
||||
if (!cmd.HasPermission(GuildPermission.ModerateMembers)) return;
|
||||
|
||||
var toUnmute = cmd.GetMember(args, 0, "ToUnmute");
|
||||
var toUnmute = cmd.GetMember(args, cleanArgs, 0, "ToUnmute");
|
||||
var reason = cmd.GetRemaining(args, 1, "UnmuteReason");
|
||||
if (toUnmute == null || reason == null || !cmd.CanInteractWith(toUnmute, "Unmute")) return;
|
||||
await UnmuteMember(cmd, toUnmute, reason);
|
||||
await UnmuteMemberAsync(cmd, toUnmute, reason);
|
||||
}
|
||||
|
||||
public static async Task UnmuteMember(CommandProcessor cmd, SocketGuildUser toUnmute,
|
||||
public static async Task UnmuteMemberAsync(CommandProcessor cmd, SocketGuildUser toUnmute,
|
||||
string reason) {
|
||||
var requestOptions = Utils.GetRequestOptions($"({cmd.Context.User}) {reason}");
|
||||
var role = Utils.GetMuteRole(cmd.Context.Guild);
|
||||
|
@ -31,8 +31,8 @@ public class UnmuteCommand : Command {
|
|||
|
||||
await toUnmute.RemoveRoleAsync(role, requestOptions);
|
||||
} else {
|
||||
if (toUnmute.TimedOutUntil == null || toUnmute.TimedOutUntil.Value.ToUnixTimeMilliseconds() <
|
||||
DateTimeOffset.Now.ToUnixTimeMilliseconds()) {
|
||||
if (toUnmute.TimedOutUntil == null || toUnmute.TimedOutUntil.Value.ToUnixTimeSeconds() <
|
||||
DateTimeOffset.Now.ToUnixTimeSeconds()) {
|
||||
cmd.Reply(Messages.MemberNotMuted, ":x: ");
|
||||
return;
|
||||
}
|
||||
|
@ -44,4 +44,4 @@ public class UnmuteCommand : Command {
|
|||
cmd.Reply(feedback, ":loud_sound: ");
|
||||
cmd.Audit(feedback);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ using Discord.WebSocket;
|
|||
|
||||
namespace Boyfriend;
|
||||
|
||||
public class EventHandler {
|
||||
public sealed class EventHandler {
|
||||
private readonly DiscordSocketClient _client = Boyfriend.Client;
|
||||
|
||||
public void InitEvents() {
|
||||
|
@ -50,7 +50,7 @@ public class EventHandler {
|
|||
if (auditLogEntry.Data is MessageDeleteAuditLogData data && msg.Author.Id == data.Target.Id)
|
||||
mention = auditLogEntry.User.Mention;
|
||||
|
||||
await Utils.SendFeedback(
|
||||
await Utils.SendFeedbackAsync(
|
||||
string.Format(Messages.CachedMessageDeleted, msg.Author.Mention, Utils.MentionChannel(channel.Id),
|
||||
Utils.Wrap(msg.CleanContent)), guild.Id, mention);
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ public class EventHandler {
|
|||
(message.Content.Contains(prev) || message.Content.Contains(prevFailsafe))))
|
||||
return;
|
||||
|
||||
_ = new CommandProcessor(message).HandleCommand();
|
||||
_ = new CommandProcessor(message).HandleCommandAsync();
|
||||
}
|
||||
|
||||
private static async Task MessageUpdatedEvent(Cacheable<IMessage, ulong> messageCached, SocketMessage messageSocket,
|
||||
|
@ -98,7 +98,7 @@ public class EventHandler {
|
|||
|
||||
var isLimitedSpace = msg.CleanContent.Length + messageSocket.CleanContent.Length < 1940;
|
||||
|
||||
await Utils.SendFeedback(
|
||||
await Utils.SendFeedbackAsync(
|
||||
string.Format(Messages.CachedMessageEdited, Utils.MentionChannel(channel.Id),
|
||||
Utils.Wrap(msg.CleanContent, isLimitedSpace), Utils.Wrap(messageSocket.CleanContent, isLimitedSpace)),
|
||||
guildId, msg.Author.Mention);
|
||||
|
@ -176,4 +176,4 @@ public class EventHandler {
|
|||
await channel.SendMessageAsync(string.Format(Messages.EventCompleted, Utils.Wrap(scheduledEvent.Name),
|
||||
Utils.Wrap(scheduledEvent.StartTime.Subtract(DateTimeOffset.Now).Negate().ToString())));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using Boyfriend.Commands;
|
||||
using Discord;
|
||||
using Discord.Net;
|
||||
using Discord.WebSocket;
|
||||
|
@ -55,10 +56,6 @@ public static class Utils {
|
|||
return user;
|
||||
}
|
||||
|
||||
public static SocketGuildUser? ParseMember(SocketGuild guild, string mention) {
|
||||
return guild.GetUser(ParseMention(mention));
|
||||
}
|
||||
|
||||
public static async Task SendDirectMessage(SocketUser user, string toSend) {
|
||||
try { await user.SendMessageAsync(toSend); } catch (HttpException e) {
|
||||
if (e.DiscordCode != DiscordErrorCode.CannotSendMessageToUser)
|
||||
|
@ -115,7 +112,8 @@ public static class Utils {
|
|||
return toReturn;
|
||||
}
|
||||
|
||||
public static async Task SendFeedback(string feedback, ulong guildId, string mention, bool sendPublic = false) {
|
||||
public static async Task
|
||||
SendFeedbackAsync(string feedback, ulong guildId, string mention, bool sendPublic = false) {
|
||||
var adminChannel = GetAdminLogChannel(guildId);
|
||||
var systemChannel = Boyfriend.Client.GetGuild(guildId).SystemChannel;
|
||||
var toSend = string.Format(Messages.FeedbackFormat, mention, feedback);
|
||||
|
@ -153,4 +151,15 @@ public static class Utils {
|
|||
|
||||
appendTo.AppendLine(appendWhat);
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task DelayedUnbanAsync(CommandProcessor cmd, ulong banned, string reason, TimeSpan duration) {
|
||||
await Task.Delay(duration);
|
||||
await UnbanCommand.UnbanUserAsync(cmd, banned, reason);
|
||||
}
|
||||
|
||||
public static async Task DelayedUnmuteAsync(CommandProcessor cmd, SocketGuildUser muted, string reason,
|
||||
TimeSpan duration) {
|
||||
await Task.Delay(duration);
|
||||
await UnmuteCommand.UnmuteMemberAsync(cmd, muted, reason);
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue