Skip to content

Commit

Permalink
wolfram can now be used as a user command, add-raw and remove-raw fil…
Browse files Browse the repository at this point in the history
…tered channel commands
  • Loading branch information
SquirrelKiev committed Apr 25, 2024
1 parent f0af46b commit ef53350
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 6 deletions.
5 changes: 4 additions & 1 deletion Asahi/Modules/Common/HasWolframPermissions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ public override Task<PreconditionResult> CheckRequirementsAsync(IInteractionCont
{
var config = services.GetRequiredService<BotConfig>();

var isTrusted = config.WolframTrustedIds.Contains(context.User.Id) || config.WolframTrustedIds.Contains(context.Channel.Id);
var isTrusted = config.WolframTrustedIds.Contains(context.User.Id);

if(!isTrusted && context.Channel != null)
isTrusted = isTrusted || config.WolframTrustedIds.Contains(context.Channel.Id);

if (!isTrusted && context.Guild != null)
isTrusted = isTrusted || config.WolframTrustedIds.Contains(context.Guild.Id);
Expand Down
1 change: 1 addition & 0 deletions Asahi/Modules/CustomCommands/CustomCommandModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Asahi.Modules.CustomCommands;

[CommandContextType(InteractionContextType.Guild)]
[Group("commands", "Stuff related to custom commands.")]
public class CustomCommandModule(CustomCommandService commandService, InteractiveService interactiveService) : BotModule
{
Expand Down
171 changes: 168 additions & 3 deletions Asahi/Modules/Highlights/HighlightsModule.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Text.RegularExpressions;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Channels;
using Asahi.Database;
using Asahi.Database.Models;
using Discord.Interactions;
Expand All @@ -10,9 +13,9 @@

namespace Asahi.Modules.Highlights;

[Group("highlights", "Commands relating to the highlights system.")]
[InteractionsModCommand]
[RequireContext(ContextType.Guild)]
[CommandContextType(InteractionContextType.Guild)]
[Group("highlights", "Commands relating to the highlights system.")]
public class HighlightsModule(DbService dbService, HighlightsTrackingService hts, ILogger<HighlightsModule> logger) : HighlightsSubmodule(dbService)
{
#region Create/Remove board
Expand Down Expand Up @@ -479,6 +482,168 @@ public Task AddFilterChannelSlash(
});
}

[SlashCommand("add-raw",
"Add multiple channels to the channel filter. This takes channel IDs. Comma separated.")]
public Task AddFilterChannelsRawSlash(
[Summary(description: "The name/ID of the board. Case insensitive.")]
[MaxLength(HighlightBoard.MaxNameLength)]
[Autocomplete(typeof(HighlightsNameAutocomplete))]
string name,
string channels)
{
return CommonBoardConfig(name, async options =>
{
List<ulong> addedChannels = [];
List<ulong> failedChannelsBecauseNull = [];
List<ulong> failedChannelsBecauseAlreadyExists = [];
List<string> failedChannelsBecauseInvalidUlong = [];
foreach (var channelStr in channels.Split(","))
{
if (!ulong.TryParse(channelStr, out var channelId))
{
failedChannelsBecauseInvalidUlong.Add(channelStr);
continue;
}

var channel = await Context.Guild.GetChannelAsync(channelId);

if (channel == null)
{
failedChannelsBecauseNull.Add(channelId);
continue;
}
if (options.board.FilteredChannels.Contains(channel.Id))
{
failedChannelsBecauseAlreadyExists.Add(channelId);
continue;
}

options.board.FilteredChannels.Add(channel.Id);
addedChannels.Add(channel.Id);
}

var sb = new StringBuilder();
if (addedChannels.Count != 0)
{
sb.AppendLine().Append("Added the following channels: ");
foreach (var channel in addedChannels)
{
sb.Append("<#").Append(channel).Append(">, ");
}
}

if (failedChannelsBecauseNull.Count != 0)
{
sb.AppendLine().Append("Failed to add the following channels as they could not be found: ");
foreach (var channel in failedChannelsBecauseNull)
{
sb.Append("<#").Append(channel).Append(">, ");
}
}

if (failedChannelsBecauseAlreadyExists.Count != 0)
{
sb.AppendLine().Append("Failed to add the following channels as they are already added: ");
foreach (var channel in failedChannelsBecauseAlreadyExists)
{
sb.Append("<#").Append(channel).Append(">, ");
}
}

if (failedChannelsBecauseInvalidUlong.Count != 0)
{
sb.AppendLine().Append("Failed to add the following channels as they could not be parsed: ");
foreach (var channel in failedChannelsBecauseInvalidUlong)
{
sb.Append(channel).Append(", ");
}
}

return new ConfigChangeResult(true, sb.ToString());
});
}

[SlashCommand("remove-raw",
"Remove multiple channels to the channel filter. This takes channel IDs. Comma separated.")]
public Task RemoveFilterChannelsRawSlash(
[Summary(description: "The name/ID of the board. Case insensitive.")]
[MaxLength(HighlightBoard.MaxNameLength)]
[Autocomplete(typeof(HighlightsNameAutocomplete))]
string name,
string channels)
{
return CommonBoardConfig(name, async options =>
{
List<ulong> removedChannels = [];
List<ulong> failedChannelsBecauseNull = [];
List<ulong> failedChannelsBecauseDoesntExist = [];
List<string> failedChannelsBecauseInvalidUlong = [];
foreach (var channelStr in channels.Split(","))
{
if (!ulong.TryParse(channelStr, out var channelId))
{
failedChannelsBecauseInvalidUlong.Add(channelStr);
continue;
}

var channel = await Context.Guild.GetChannelAsync(channelId);

if (channel == null)
{
failedChannelsBecauseNull.Add(channelId);
continue;
}
if (!options.board.FilteredChannels.Contains(channel.Id))
{
failedChannelsBecauseDoesntExist.Add(channelId);
continue;
}

options.board.FilteredChannels.Remove(channel.Id);
removedChannels.Add(channel.Id);
}

var sb = new StringBuilder();
if (removedChannels.Count != 0)
{
sb.AppendLine().Append("Removed the following channels: ");
foreach (var channel in removedChannels)
{
sb.Append("<#").Append(channel).Append(">, ");
}
}

if (failedChannelsBecauseNull.Count != 0)
{
sb.AppendLine().Append("Failed to remove the following channels as they could not be found: ");
foreach (var channel in failedChannelsBecauseNull)
{
sb.Append("<#").Append(channel).Append(">, ");
}
}

if (failedChannelsBecauseDoesntExist.Count != 0)
{
sb.AppendLine().Append("Failed to remove the following channels as they weren't filtered anyway: ");
foreach (var channel in failedChannelsBecauseDoesntExist)
{
sb.Append("<#").Append(channel).Append(">, ");
}
}

if (failedChannelsBecauseInvalidUlong.Count != 0)
{
sb.AppendLine().Append("Failed to remove the following channels as they could not be parsed: ");
foreach (var channel in failedChannelsBecauseInvalidUlong)
{
sb.Append(channel).Append(", ");
}
}

return new ConfigChangeResult(true, sb.ToString());
});
}

[SlashCommand("remove",
"Removes the channel from the channel filter. Channel filter is blocklist by default.")]
public Task RemoveFilterChannelSlash(
Expand Down
3 changes: 2 additions & 1 deletion Asahi/Modules/LazyConfig/LazyConfigModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

namespace Asahi.Modules.LazyConfig;

[CommandContextType(InteractionContextType.Guild)]
// [Group("config", "Configuration commands.")]
public partial class LazyConfigModule(DbService dbService) : BotModule
public class LazyConfigModule(DbService dbService) : BotModule
{
[SlashCommand("prefix", "Gets/sets the bot prefix.")]
[InteractionsModCommand]
Expand Down
3 changes: 2 additions & 1 deletion Asahi/Modules/Seigen/TrackablesModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

namespace Asahi.Modules.Seigen;

[Group("trackables", "Commands relating to managing trackables and their users.")]
[InteractionsModCommand]
[CommandContextType(InteractionContextType.Guild)]
[Group("trackables", "Commands relating to managing trackables and their users.")]
public class TrackablesModule(DbService dbService, RoleManagementService roleManagement, TrackablesUtility trackablesUtility) : BotModule
{
public const string MONITORED_GUILD_PARAM_NAME = "monitored-guild";
Expand Down
1 change: 1 addition & 0 deletions Asahi/Modules/WolframAlpha/WolframModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Asahi.Modules.WolframAlpha;
// I can't be bothered to deal with the full results api.
[HasWolframPermissions]
[CommandContextType(InteractionContextType.Guild, InteractionContextType.BotDm, InteractionContextType.PrivateChannel)]
[IntegrationType(ApplicationIntegrationType.GuildInstall, ApplicationIntegrationType.UserInstall)]
public class WolframModule(BotConfig botConfig, HttpClient httpClient) : BotModule
{
public enum DisplayType
Expand Down
1 change: 1 addition & 0 deletions Asahi/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public static async Task Main(string[] args)
builder.Logging.AddSerilog(logger);

builder.Services.AddBotServices(botConfig);
builder.Logging.AddFilter("Microsoft.Extensions.Http.DefaultHttpClientFactory", LogLevel.None);

builder.Services.AddHostedService<BotService>();

Expand Down

0 comments on commit ef53350

Please sign in to comment.