1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 09:09:00 +03:00

Keep reply emojis as consts in a separate class (#8)

This commit is contained in:
Macintxsh 2022-12-06 18:33:46 +03:00 committed by GitHub
parent 1258496697
commit 2596b48bde
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 65 additions and 52 deletions

View file

@ -7,12 +7,6 @@ using Discord.WebSocket;
namespace Boyfriend;
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: ";
private static readonly string Mention = $"<@{Boyfriend.Client.CurrentUser.Id}>";
public static readonly ICommand[] Commands = {
@ -79,7 +73,7 @@ public sealed class CommandProcessor {
}
public void Reply(string response, string? customEmoji = null) {
Utils.SafeAppendToBuilder(_stackedReplyMessage, $"{customEmoji ?? Success}{response}", Context.Message);
Utils.SafeAppendToBuilder(_stackedReplyMessage, $"{customEmoji ?? ReplyEmojis.Success} {response}", Context.Message);
}
public void Audit(string action, bool isPublic = true) {
@ -111,14 +105,14 @@ public sealed class CommandProcessor {
public string? GetRemaining(string[] from, int startIndex, string? argument) {
if (startIndex >= from.Length && argument is not null)
Utils.SafeAppendToBuilder(_stackedReplyMessage,
$"{MissingArgument}{Utils.GetMessage($"Missing{argument}")}", Context.Message);
$"{ReplyEmojis.MissingArgument} {Utils.GetMessage($"Missing{argument}")}", Context.Message);
else return string.Join(" ", from, startIndex, from.Length - startIndex);
return null;
}
public SocketUser? GetUser(string[] args, string[] cleanArgs, int index, string? argument) {
if (index >= args.Length) {
Utils.SafeAppendToBuilder(_stackedReplyMessage, $"{MissingArgument}{Messages.MissingUser}",
Utils.SafeAppendToBuilder(_stackedReplyMessage, $"{ReplyEmojis.MissingArgument} {Messages.MissingUser}",
Context.Message);
return null;
}
@ -126,14 +120,14 @@ public sealed class CommandProcessor {
var user = Boyfriend.Client.GetUser(Utils.ParseMention(args[index]));
if (user is null && argument is not null)
Utils.SafeAppendToBuilder(_stackedReplyMessage,
$"{InvalidArgument}{string.Format(Messages.InvalidUser, Utils.Wrap(cleanArgs[index]))}",
$"{ReplyEmojis.InvalidArgument} {string.Format(Messages.InvalidUser, Utils.Wrap(cleanArgs[index]))}",
Context.Message);
return user;
}
public bool HasPermission(GuildPermission permission) {
if (!Context.Guild.CurrentUser.GuildPermissions.Has(permission)) {
Utils.SafeAppendToBuilder(_stackedReplyMessage, $"{NoAccess}{Utils.GetMessage($"BotCannot{permission}")}",
Utils.SafeAppendToBuilder(_stackedReplyMessage, $"{ReplyEmojis.NoPermission} {Utils.GetMessage($"BotCannot{permission}")}",
Context.Message);
return false;
}
@ -141,7 +135,7 @@ public sealed class CommandProcessor {
if (Context.Guild.GetUser(Context.User.Id).GuildPermissions.Has(permission)
|| Context.Guild.OwnerId == Context.User.Id) return true;
Utils.SafeAppendToBuilder(_stackedReplyMessage, $"{NoAccess}{Utils.GetMessage($"UserCannot{permission}")}",
Utils.SafeAppendToBuilder(_stackedReplyMessage, $"{ReplyEmojis.NoPermission} {Utils.GetMessage($"UserCannot{permission}")}",
Context.Message);
return false;
}
@ -152,7 +146,7 @@ public sealed class CommandProcessor {
public SocketGuildUser? GetMember(string[] args, string[] cleanArgs, int index, string? argument) {
if (index >= args.Length) {
Utils.SafeAppendToBuilder(_stackedReplyMessage, $"{MissingArgument}{Messages.MissingMember}",
Utils.SafeAppendToBuilder(_stackedReplyMessage, $"{ReplyEmojis.MissingArgument} {Messages.MissingMember}",
Context.Message);
return null;
}
@ -160,7 +154,7 @@ public sealed class CommandProcessor {
var member = Context.Guild.GetUser(Utils.ParseMention(args[index]));
if (member is null && argument is not null)
Utils.SafeAppendToBuilder(_stackedReplyMessage,
$"{InvalidArgument}{string.Format(Messages.InvalidMember, Utils.Wrap(cleanArgs[index]))}",
$"{ReplyEmojis.InvalidArgument} {string.Format(Messages.InvalidMember, Utils.Wrap(cleanArgs[index]))}",
Context.Message);
return member;
}
@ -171,7 +165,7 @@ public sealed class CommandProcessor {
public ulong? GetBan(string[] args, int index) {
if (index >= args.Length) {
Utils.SafeAppendToBuilder(_stackedReplyMessage, $"{MissingArgument}{Messages.MissingUser}",
Utils.SafeAppendToBuilder(_stackedReplyMessage, $"{ReplyEmojis.MissingArgument} {Messages.MissingUser}",
Context.Message);
return null;
}
@ -188,14 +182,14 @@ public sealed class CommandProcessor {
public int? GetNumberRange(string[] args, int index, int min, int max, string? argument) {
if (index >= args.Length) {
Utils.SafeAppendToBuilder(_stackedReplyMessage,
$"{MissingArgument}{string.Format(Messages.MissingNumber, min.ToString(), max.ToString())}",
$"{ReplyEmojis.MissingArgument} {string.Format(Messages.MissingNumber, min.ToString(), max.ToString())}",
Context.Message);
return null;
}
if (!int.TryParse(args[index], out var i)) {
Utils.SafeAppendToBuilder(_stackedReplyMessage,
$"{InvalidArgument}{string.Format(Utils.GetMessage($"{argument}Invalid"), min.ToString(), max.ToString(), Utils.Wrap(args[index]))}",
$"{ReplyEmojis.InvalidArgument} {string.Format(Utils.GetMessage($"{argument}Invalid"), min.ToString(), max.ToString(), Utils.Wrap(args[index]))}",
Context.Message);
return null;
}
@ -203,14 +197,14 @@ public sealed class CommandProcessor {
if (argument is null) return i;
if (i < min) {
Utils.SafeAppendToBuilder(_stackedReplyMessage,
$"{InvalidArgument}{string.Format(Utils.GetMessage($"{argument}TooSmall"), min.ToString())}",
$"{ReplyEmojis.InvalidArgument} {string.Format(Utils.GetMessage($"{argument}TooSmall"), min.ToString())}",
Context.Message);
return null;
}
if (i <= max) return i;
Utils.SafeAppendToBuilder(_stackedReplyMessage,
$"{InvalidArgument}{string.Format(Utils.GetMessage($"{argument}TooLarge"), max.ToString())}",
$"{ReplyEmojis.InvalidArgument} {string.Format(Utils.GetMessage($"{argument}TooLarge"), max.ToString())}",
Context.Message);
return null;
}
@ -252,31 +246,31 @@ public sealed class CommandProcessor {
public bool CanInteractWith(SocketGuildUser user, string action) {
if (Context.User.Id == user.Id) {
Utils.SafeAppendToBuilder(_stackedReplyMessage,
$"{CantInteract}{Utils.GetMessage($"UserCannot{action}Themselves")}", Context.Message);
$"{ReplyEmojis.CantInteract} {Utils.GetMessage($"UserCannot{action}Themselves")}", Context.Message);
return false;
}
if (Context.Guild.CurrentUser.Id == user.Id) {
Utils.SafeAppendToBuilder(_stackedReplyMessage,
$"{CantInteract}{Utils.GetMessage($"UserCannot{action}Bot")}", Context.Message);
$"{ReplyEmojis.CantInteract} {Utils.GetMessage($"UserCannot{action}Bot")}", Context.Message);
return false;
}
if (Context.Guild.Owner.Id == user.Id) {
Utils.SafeAppendToBuilder(_stackedReplyMessage,
$"{CantInteract}{Utils.GetMessage($"UserCannot{action}Owner")}", Context.Message);
$"{ReplyEmojis.CantInteract} {Utils.GetMessage($"UserCannot{action}Owner")}", Context.Message);
return false;
}
if (Context.Guild.CurrentUser.Hierarchy <= user.Hierarchy) {
Utils.SafeAppendToBuilder(_stackedReplyMessage,
$"{CantInteract}{Utils.GetMessage($"BotCannot{action}Target")}", Context.Message);
$"{ReplyEmojis.CantInteract} {Utils.GetMessage($"BotCannot{action}Target")}", Context.Message);
return false;
}
if (Context.Guild.Owner.Id == Context.User.Id || GetMember().Hierarchy > user.Hierarchy) return true;
Utils.SafeAppendToBuilder(_stackedReplyMessage,
$"{CantInteract}{Utils.GetMessage($"UserCannot{action}Target")}", Context.Message);
$"{ReplyEmojis.CantInteract} {Utils.GetMessage($"UserCannot{action}Target")}", Context.Message);
return false;
}
}

View file

@ -1,4 +1,4 @@
using Discord;
using Discord;
using Discord.WebSocket;
namespace Boyfriend.Commands;
@ -29,7 +29,7 @@ public sealed class BanCommand : ICommand {
var feedback = string.Format(Messages.FeedbackUserBanned, toBan.Mention,
Utils.GetHumanizedTimeOffset(duration), Utils.Wrap(reason));
cmd.Reply(feedback, ":hammer: ");
cmd.Reply(feedback, ReplyEmojis.Banned);
cmd.Audit(feedback);
if (duration.TotalSeconds > 0)

View file

@ -1,4 +1,4 @@
using Humanizer;
using Humanizer;
namespace Boyfriend.Commands;
@ -12,7 +12,7 @@ public sealed class HelpCommand : ICommand {
foreach (var command in CommandProcessor.Commands)
toSend.Append(
$"\n`{prefix}{command.Aliases[0]}`: {Utils.GetMessage($"CommandDescription{command.Aliases[0].Titleize()}")}");
cmd.Reply(toSend.ToString(), ":page_facing_up: ");
cmd.Reply(toSend.ToString(), ReplyEmojis.Help);
toSend.Clear();
return Task.CompletedTask;

View file

@ -1,4 +1,4 @@
using Discord;
using Discord;
using Discord.WebSocket;
namespace Boyfriend.Commands;
@ -24,7 +24,7 @@ public sealed class KickCommand : ICommand {
await toKick.KickAsync(guildKickMessage);
var format = string.Format(Messages.FeedbackMemberKicked, toKick.Mention, Utils.Wrap(reason));
cmd.Reply(format, ":police_car: ");
cmd.Reply(format, ReplyEmojis.Kicked);
cmd.Audit(format);
}
}

View file

@ -1,4 +1,4 @@
using Discord;
using Discord;
using Discord.Net;
using Discord.WebSocket;
@ -20,7 +20,7 @@ public sealed class MuteCommand : ICommand {
|| (toMute.TimedOutUntil is not null
&& toMute.TimedOutUntil.Value.ToUnixTimeSeconds()
> DateTimeOffset.Now.ToUnixTimeSeconds())) {
cmd.Reply(Messages.MemberAlreadyMuted, ":x: ");
cmd.Reply(Messages.MemberAlreadyMuted, ReplyEmojis.Error);
return;
}
@ -30,7 +30,7 @@ public sealed class MuteCommand : ICommand {
foreach (var roleId in mutedRemovedRoles) await toMute.AddRoleAsync(roleId);
rolesRemoved.Remove(toMute.Id);
cmd.ConfigWriteScheduled = true;
cmd.Reply(Messages.RolesReturned, ":warning: ");
cmd.Reply(Messages.RolesReturned, ReplyEmojis.Warning);
}
if (cmd.HasPermission(GuildPermission.ModerateMembers) && cmd.CanInteractWith(toMute, "Mute"))
@ -55,7 +55,7 @@ public sealed class MuteCommand : ICommand {
rolesRemoved.Add(userRole.Id);
} catch (HttpException e) {
cmd.Reply(string.Format(Messages.RoleRemovalFailed, $"<@&{userRole}>", Utils.Wrap(e.Reason)),
":warning: ");
ReplyEmojis.Warning);
}
Boyfriend.GetRemovedRoles(guild.Id).Add(toMute.Id, rolesRemoved.AsReadOnly());
@ -68,12 +68,12 @@ public sealed class MuteCommand : ICommand {
await Task.FromResult(Utils.DelayedUnmuteAsync(cmd, toMute, Messages.PunishmentExpired, duration));
} else {
if (!hasDuration || duration.TotalDays > 28) {
cmd.Reply(Messages.DurationRequiredForTimeOuts, ":x: ");
cmd.Reply(Messages.DurationRequiredForTimeOuts, ReplyEmojis.Error);
return;
}
if (toMute.IsBot) {
cmd.Reply(Messages.CannotTimeOutBot, ":x: ");
cmd.Reply(Messages.CannotTimeOutBot, ReplyEmojis.Error);
return;
}
@ -83,7 +83,7 @@ public sealed class MuteCommand : ICommand {
var feedback = string.Format(Messages.FeedbackMemberMuted, toMute.Mention,
Utils.GetHumanizedTimeOffset(duration),
Utils.Wrap(reason));
cmd.Reply(feedback, ":mute: ");
cmd.Reply(feedback, ReplyEmojis.Muted);
cmd.Audit(feedback);
}
}

View file

@ -1,4 +1,4 @@
namespace Boyfriend.Commands;
namespace Boyfriend.Commands;
public sealed class PingCommand : ICommand {
public string[] Aliases { get; } = { "ping", "latency", "pong", "пинг", "задержка", "понг" };
@ -10,7 +10,7 @@ public sealed class PingCommand : ICommand {
.Append(Math.Abs(DateTimeOffset.Now.Subtract(cmd.Context.Message.Timestamp).TotalMilliseconds))
.Append(Messages.Milliseconds);
cmd.Reply(builder.ToString(), ":signal_strength: ");
cmd.Reply(builder.ToString(), ReplyEmojis.Ping);
builder.Clear();
return Task.CompletedTask;

View file

@ -1,4 +1,4 @@
using Discord;
using Discord;
namespace Boyfriend.Commands;
@ -33,7 +33,7 @@ public sealed class SettingsCommand : ICommand {
.AppendFormat(format, currentValue).AppendLine();
}
cmd.Reply(currentSettings.ToString(), ":gear: ");
cmd.Reply(currentSettings.ToString(), ReplyEmojis.SettingsList);
currentSettings.Clear();
return Task.CompletedTask;
}
@ -51,7 +51,7 @@ public sealed class SettingsCommand : ICommand {
}
if (!exists) {
cmd.Reply(Messages.SettingDoesntExist, ":x: ");
cmd.Reply(Messages.SettingDoesntExist, ReplyEmojis.Error);
return Task.CompletedTask;
}
@ -64,7 +64,7 @@ public sealed class SettingsCommand : ICommand {
value = value.Replace(" ", "").ToLower();
if (value.StartsWith(",") || value.Count(x => x is ',') > 1 ||
(!value.Contains("interested") && !value.Contains("users") && !value.Contains("role"))) {
cmd.Reply(Messages.InvalidSettingValue, ":x: ");
cmd.Reply(Messages.InvalidSettingValue, ReplyEmojis.Error);
return Task.CompletedTask;
}
}
@ -77,7 +77,7 @@ public sealed class SettingsCommand : ICommand {
_ => value
};
if (!IsBool(value)) {
cmd.Reply(Messages.InvalidSettingValue, ":x: ");
cmd.Reply(Messages.InvalidSettingValue, ReplyEmojis.Error);
return Task.CompletedTask;
}
}
@ -106,22 +106,22 @@ public sealed class SettingsCommand : ICommand {
} else {
if (value == config[selectedSetting]) {
cmd.Reply(string.Format(Messages.SettingsNothingChanged, localizedSelectedSetting, formattedValue),
":x: ");
ReplyEmojis.Error);
return Task.CompletedTask;
}
if (selectedSetting is "Lang" && !Utils.CultureInfoCache.ContainsKey(value)) {
cmd.Reply(Messages.LanguageNotSupported, ":x: ");
cmd.Reply(Messages.LanguageNotSupported, ReplyEmojis.Error);
return Task.CompletedTask;
}
if (selectedSetting.EndsWith("Channel") && guild.GetTextChannel(mention) is null) {
cmd.Reply(Messages.InvalidChannel, ":x: ");
cmd.Reply(Messages.InvalidChannel, ReplyEmojis.Error);
return Task.CompletedTask;
}
if (selectedSetting.EndsWith("Role") && guild.GetRole(mention) is null) {
cmd.Reply(Messages.InvalidRole, ":x: ");
cmd.Reply(Messages.InvalidRole, ReplyEmojis.Error);
return Task.CompletedTask;
}
@ -138,7 +138,7 @@ public sealed class SettingsCommand : ICommand {
cmd.ConfigWriteScheduled = true;
var replyFormat = string.Format(Messages.FeedbackSettingsUpdated, localizedSelectedSetting, formattedValue);
cmd.Reply(replyFormat, ":control_knobs: ");
cmd.Reply(replyFormat, ReplyEmojis.SettingsSet);
cmd.Audit(replyFormat, false);
return Task.CompletedTask;
}

View file

@ -1,4 +1,4 @@
using Discord;
using Discord;
using Discord.WebSocket;
namespace Boyfriend.Commands;
@ -34,7 +34,7 @@ public sealed class UnmuteCommand : ICommand {
} else {
if (toUnmute.TimedOutUntil is null || toUnmute.TimedOutUntil.Value.ToUnixTimeSeconds() <
DateTimeOffset.Now.ToUnixTimeSeconds()) {
cmd.Reply(Messages.MemberNotMuted, ":x: ");
cmd.Reply(Messages.MemberNotMuted, ReplyEmojis.Error);
return;
}
@ -42,7 +42,7 @@ public sealed class UnmuteCommand : ICommand {
}
var feedback = string.Format(Messages.FeedbackMemberUnmuted, toUnmute.Mention, Utils.Wrap(reason));
cmd.Reply(feedback, ":loud_sound: ");
cmd.Reply(feedback, ReplyEmojis.Unmuted);
cmd.Audit(feedback);
}
}

19
Boyfriend/ReplyEmojis.cs Normal file
View file

@ -0,0 +1,19 @@
namespace Boyfriend;
public static class ReplyEmojis {
public const string Success = ":white_check_mark:";
public const string Warning = ":warning: ";
public const string Error = ":x:";
public const string MissingArgument = ":keyboard:";
public const string InvalidArgument = ":construction:";
public const string NoPermission = ":no_entry_sign:";
public const string CantInteract = ":vertical_traffic_light:";
public const string Help = ":page_facing_up:";
public const string SettingsList = ":gear:";
public const string SettingsSet = ":control_knobs:";
public const string Ping = ":signal_strength:";
public const string Banned = ":hammer:";
public const string Kicked = ":police_car:";
public const string Muted = ":mute:";
public const string Unmuted = ":loud_sound:";
}