Allow mentions to act as command prefixes + bug fixes

This commit is contained in:
l1ttleO 2022-08-05 21:01:06 +05:00
parent 2493e317d0
commit b2b54b7fd4
Signed by: Octol1ttle
GPG key ID: B77C34313AEE1FFF
3 changed files with 15 additions and 20 deletions

View file

@ -15,6 +15,7 @@ public static class CommandHandler {
}; };
private static readonly Dictionary<string, Regex> RegexCache = new(); private static readonly Dictionary<string, Regex> RegexCache = new();
private static readonly Regex MentionRegex = new(Regex.Escape("<@855023234407333888>"));
public static readonly StringBuilder StackedReplyMessage = new(); public static readonly StringBuilder StackedReplyMessage = new();
public static readonly StringBuilder StackedPublicFeedback = new(); public static readonly StringBuilder StackedPublicFeedback = new();
@ -43,7 +44,8 @@ public static class CommandHandler {
foreach (var line in list) { foreach (var line in list) {
currentLine++; currentLine++;
foreach (var command in Commands) { foreach (var command in Commands) {
if (!command.Aliases.Contains(regex.Replace(line, "", 1).ToLower().Split()[0])) var lineNoMention = MentionRegex.Replace(line, "", 1);
if (!command.Aliases.Contains(regex.Replace(lineNoMention, "", 1).Trim().ToLower().Split()[0]))
continue; continue;
await context.Channel.TriggerTypingAsync(); await context.Channel.TriggerTypingAsync();
@ -58,6 +60,7 @@ public static class CommandHandler {
if (currentLine != list.Length) continue; if (currentLine != list.Length) continue;
if (ConfigWriteScheduled) await Boyfriend.WriteGuildConfig(guild.Id); if (ConfigWriteScheduled) await Boyfriend.WriteGuildConfig(guild.Id);
if (StackedReplyMessage.Length > 0)
await message.ReplyAsync(StackedReplyMessage.ToString(), false, null, AllowedMentions.None); await message.ReplyAsync(StackedReplyMessage.ToString(), false, null, AllowedMentions.None);
var adminChannel = Utils.GetAdminLogChannel(guild.Id); var adminChannel = Utils.GetAdminLogChannel(guild.Id);

View file

@ -32,14 +32,12 @@ public class SettingsCommand : Command {
format = "<#{0}>"; format = "<#{0}>";
else else
currentValue = Messages.ChannelNotSpecified; currentValue = Messages.ChannelNotSpecified;
} } else if (setting.Key.EndsWith("Role")) {
else if (setting.Key.EndsWith("Role")) {
if (guild.GetRole(Convert.ToUInt64(currentValue)) != null) if (guild.GetRole(Convert.ToUInt64(currentValue)) != null)
format = "<@&{0}>"; format = "<@&{0}>";
else else
currentValue = Messages.RoleNotSpecified; currentValue = Messages.RoleNotSpecified;
} } else {
else {
if (IsBool(currentValue)) if (IsBool(currentValue))
currentValue = YesOrNo(currentValue is "true"); currentValue = YesOrNo(currentValue is "true");
else else
@ -84,10 +82,7 @@ public class SettingsCommand : Command {
return Task.CompletedTask; return Task.CompletedTask;
} }
} }
} } else { value = "reset"; }
else {
value = "reset";
}
if (IsBool(Boyfriend.DefaultConfig[selectedSetting]) && !IsBool(value)) { if (IsBool(Boyfriend.DefaultConfig[selectedSetting]) && !IsBool(value)) {
value = value switch { value = value switch {
@ -117,9 +112,9 @@ public class SettingsCommand : Command {
var formattedValue = selectedSetting switch { var formattedValue = selectedSetting switch {
"WelcomeMessage" => Utils.Wrap(Messages.DefaultWelcomeMessage), "WelcomeMessage" => Utils.Wrap(Messages.DefaultWelcomeMessage),
"EventStartedReceivers" => Utils.Wrap(Boyfriend.DefaultConfig[selectedSetting])!, "EventStartedReceivers" => Utils.Wrap(Boyfriend.DefaultConfig[selectedSetting])!,
_ => value is "reset" or "default" _ => value is "reset" or "default" ? Messages.SettingNotDefined
? IsBool(value) ? YesOrNo(value is "true") : string.Format(formatting, value) : IsBool(value) ? YesOrNo(value is "true")
: Messages.SettingNotDefined : string.Format(formatting, value)
}; };
if (value is "reset" or "default") { if (value is "reset" or "default") {
@ -127,8 +122,7 @@ public class SettingsCommand : Command {
config[selectedSetting] = Messages.DefaultWelcomeMessage; config[selectedSetting] = Messages.DefaultWelcomeMessage;
else else
config[selectedSetting] = Boyfriend.DefaultConfig[selectedSetting]; config[selectedSetting] = Boyfriend.DefaultConfig[selectedSetting];
} } else {
else {
if (value == config[selectedSetting]) { if (value == config[selectedSetting]) {
Error(string.Format(Messages.SettingsNothingChanged, localizedSelectedSetting, formattedValue), false); Error(string.Format(Messages.SettingsNothingChanged, localizedSelectedSetting, formattedValue), false);
return Task.CompletedTask; return Task.CompletedTask;

View file

@ -1,6 +1,5 @@
using Boyfriend.Commands; using Boyfriend.Commands;
using Discord; using Discord;
using Discord.Commands;
using Discord.Rest; using Discord.Rest;
using Discord.WebSocket; using Discord.WebSocket;
@ -82,9 +81,8 @@ public class EventHandler {
prevFailsafe = prevsArray[2].Content; prevFailsafe = prevsArray[2].Content;
} }
if (!(message.HasStringPrefix(guildConfig["Prefix"], ref argPos) || if (user == guild.CurrentUser || (user.IsBot &&
message.HasMentionPrefix(Boyfriend.Client.CurrentUser, ref argPos)) || user == guild.CurrentUser || (message.Content.Contains(prev) || message.Content.Contains(prevFailsafe))))
(user.IsBot && (message.Content.Contains(prev) || message.Content.Contains(prevFailsafe))))
return; return;
await CommandHandler.HandleCommand(message); await CommandHandler.HandleCommand(message);