Skip to content

Commit

Permalink
Add user-installable commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Erisa committed Aug 2, 2024
1 parent 2e41e9c commit c19329f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
36 changes: 36 additions & 0 deletions src/Modules/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,41 @@ public static string UserAvatarURL(DiscordUser user, string format = "default",
return $"https://cdn.discordapp.com/avatars/{user.Id}/{user.AvatarHash}.{format}?size=4096";
}

public static async Task<string> CodeOrHasteBinAsync(string input, string language = "", int charLimit = 1930, bool plain = false)
{
bool inputHasCodeBlock = input.Contains("```");
if (input.Length > charLimit || inputHasCodeBlock)
{
HasteBinResult hasteResult = await Program.hasteUploader.Post(input);
if (hasteResult.IsSuccess)
{
var hasteUrl = hasteResult.FullUrl;
if (language != "")
{
hasteUrl = hasteUrl + "." + language;
}

if (plain)
return hasteUrl;
else if (inputHasCodeBlock)
return $"Output contained a code block, so it was uploaded to Hastebin to avoid formatting issues: {hasteUrl}";
else
return $"Output exceeded character limit: {hasteUrl}";
}
else
{
Program.discord.Logger.LogError("Error ocurred uploading to Hastebin with status code: {code}\nPayload: {output}", hasteResult.StatusCode, input);
if (plain)
return "Error, check logs.";

return $"Unknown error occurred during upload to Hastebin.\nPlease try again or contact the bot owner.";
}
}
else
{
return $"```{language}\n{input}\n```";
}
}

}
}
13 changes: 10 additions & 3 deletions src/Modules/SlashCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public async Task AvatarSlashCommand(InteractionContext ctx,
await ctx.RespondAsync(null, embed);
}

[ContextMenu(DiscordApplicationCommandType.UserContextMenu, "Show Avatar")]
[ContextMenu(DiscordApplicationCommandType.UserContextMenu, "Show Avatar"), InteractionCommandInstallType(DiscordApplicationIntegrationType.UserInstall, DiscordApplicationIntegrationType.GuildInstall), InteractionCommandAllowedContexts(DiscordInteractionContextType.PrivateChannel, DiscordInteractionContextType.Guild)]
public async Task ContextAvatar(ContextMenuContext ctx)
{
string avatarUrl = "";
Expand Down Expand Up @@ -155,16 +155,23 @@ public async Task ContextAvatar(ContextMenuContext ctx)
await ctx.RespondAsync(null, embed, ephemeral: true);
}

[ContextMenu(DiscordApplicationCommandType.UserContextMenu, "lk hug")]
[ContextMenu(DiscordApplicationCommandType.UserContextMenu, "lk hug"), InteractionCommandInstallType(DiscordApplicationIntegrationType.UserInstall, DiscordApplicationIntegrationType.GuildInstall), InteractionCommandAllowedContexts(DiscordInteractionContextType.PrivateChannel, DiscordInteractionContextType.Guild)]
public async Task ContextHug(ContextMenuContext ctx)
{
await ctx.RespondAsync($"{Program.cfgjson.Emoji.BlobHug} {ctx.TargetUser.Mention} was given a tight hug by {ctx.User.Mention}!");
}

[ContextMenu(DiscordApplicationCommandType.UserContextMenu, "lk pat")]
[ContextMenu(DiscordApplicationCommandType.UserContextMenu, "lk pat"), InteractionCommandInstallType(DiscordApplicationIntegrationType.UserInstall, DiscordApplicationIntegrationType.GuildInstall), InteractionCommandAllowedContexts(DiscordInteractionContextType.PrivateChannel, DiscordInteractionContextType.Guild)]
public async Task ContextPat(ContextMenuContext ctx)
{
await ctx.RespondAsync($"{Program.cfgjson.Emoji.BlobPats} {ctx.TargetUser.Mention} was given a big headpat by {ctx.User.Mention}!");
}

[ContextMenu(DiscordApplicationCommandType.MessageContextMenu, "Dump message data"), InteractionCommandInstallType(DiscordApplicationIntegrationType.UserInstall, DiscordApplicationIntegrationType.GuildInstall), InteractionCommandAllowedContexts(DiscordInteractionContextType.PrivateChannel, DiscordInteractionContextType.Guild)]
public async Task DumpMessage(ContextMenuContext ctx)
{
var rawMsgData = JsonConvert.SerializeObject(ctx.TargetMessage, Formatting.Indented);
await ctx.RespondAsync(await CodeOrHasteBinAsync(rawMsgData, "json"), ephemeral: true);
}
}
}
3 changes: 1 addition & 2 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,7 @@ Task Discord_ThreadMembersUpdated(DiscordClient client, ThreadMembersUpdatedEven

var slash = discord.UseSlashCommands();

Check warning on line 374 in src/Program.cs

View workflow job for this annotation

GitHub Actions / build-standalone (ubuntu-22.04)

'ExtensionMethods.UseSlashCommands(DiscordClient, SlashCommandsConfiguration)' is obsolete: 'DSharpPlus.SlashCommands is obsolete. Please consider using the new DSharpPlus.Commands extension instead.'

Check warning on line 374 in src/Program.cs

View workflow job for this annotation

GitHub Actions / build-standalone (windows-2022)

'ExtensionMethods.UseSlashCommands(DiscordClient, SlashCommandsConfiguration)' is obsolete: 'DSharpPlus.SlashCommands is obsolete. Please consider using the new DSharpPlus.Commands extension instead.'

foreach (ulong guildId in cfgjson.Guilds)
slash.RegisterCommands<SlashCommands>(guildId);
slash.RegisterCommands<SlashCommands>();

await discord.ConnectAsync();

Expand Down

0 comments on commit c19329f

Please sign in to comment.