Skip to content

Commit 88032e8

Browse files
Remove argument from /serverinfo, restrict to guild usage, add debug cmd
1 parent 3c42ea1 commit 88032e8

3 files changed

Lines changed: 76 additions & 63 deletions

File tree

Commands/DebugCommands.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,44 @@ await ctx.FollowupAsync(new DiscordFollowupMessageBuilder()
110110
[Description("Commands for managing which guilds the bot is in.")]
111111
public static class DebugGuildsCommands
112112
{
113+
[Command("info")]
114+
[Description("Get information about a guild.")]
115+
public static async Task DebugGuildsInfoCommandAsync(SlashCommandContext ctx,
116+
[Parameter("guild"), Description("The ID of the guild to get info for.")] string guildId)
117+
{
118+
await ctx.DeferResponseAsync(ephemeral: ctx.Interaction.ShouldUseEphemeralResponse(false));
119+
120+
DiscordGuild guild;
121+
122+
try
123+
{
124+
guild = await ctx.Client.GetGuildAsync(Convert.ToUInt64(guildId));
125+
}
126+
catch (FormatException)
127+
{
128+
await ctx.FollowupAsync(new DiscordFollowupMessageBuilder()
129+
.WithContent("Couldn't parse that guild ID.")
130+
.AsEphemeral(ctx.Interaction.ShouldUseEphemeralResponse(false)));
131+
return;
132+
}
133+
catch (NotFoundException)
134+
{
135+
await ctx.FollowupAsync(new DiscordFollowupMessageBuilder()
136+
.WithContent("I'm not in that server. There's nothing I can show you.")
137+
.AsEphemeral(ctx.Interaction.ShouldUseEphemeralResponse(false)));
138+
return;
139+
}
140+
catch (Exception ex)
141+
{
142+
await ctx.FollowupAsync(new DiscordFollowupMessageBuilder()
143+
.WithContent($"Something went wrong...\n```\n{ex.GetType()}: {ex.Message}\n```")
144+
.AsEphemeral(ctx.Interaction.ShouldUseEphemeralResponse(false)));
145+
return;
146+
}
147+
148+
await ctx.FollowupAsync(new DiscordFollowupMessageBuilder(await guild.CreateInfoMessageAsync())
149+
.AsEphemeral(ephemeral: ctx.Interaction.ShouldUseEphemeralResponse(false)));
150+
}
113151

114152
[Command("leave")]
115153
[Description("Makes the bot leave a guild. This cannot be undone!")]

Commands/ServerInfoCommands.cs

Lines changed: 4 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,73 +3,14 @@
33
internal class ServerInfoCommands
44
{
55
[Command("serverinfo")]
6-
[Description("Look up information about a server.")]
6+
[Description("Look up information about this server.")]
77
[InteractionInstallType(DiscordApplicationIntegrationType.GuildInstall)]
8-
public static async Task ServerInfoCommandAsync(SlashCommandContext ctx,
9-
[Parameter("server"), Description("The ID of the server to look up. Defaults to the current server if you're not using this in DMs.")]
10-
string guildId = default)
8+
[InteractionAllowedContexts(DiscordInteractionContextType.Guild)]
9+
public static async Task ServerInfoCommandAsync(SlashCommandContext ctx)
1110
{
1211
await ctx.DeferResponseAsync(ephemeral: ctx.Interaction.ShouldUseEphemeralResponse(false));
1312

14-
DiscordGuild guild;
15-
16-
if (ctx.Channel.IsPrivate && guildId == default)
17-
{
18-
await ctx.FollowupAsync(new DiscordFollowupMessageBuilder()
19-
.WithContent("You can't use this command in DMs without specifying a server ID! Please try again.")
20-
.AsEphemeral(ephemeral: ctx.Interaction.ShouldUseEphemeralResponse(false)));
21-
return;
22-
}
23-
24-
if (guildId == default)
25-
{
26-
guild = ctx.Guild;
27-
}
28-
else
29-
{
30-
try
31-
{
32-
guild = await ctx.Client.GetGuildAsync(Convert.ToUInt64(guildId));
33-
}
34-
catch (Exception ex) when (ex is UnauthorizedException or NotFoundException)
35-
{
36-
await ctx.FollowupAsync(new DiscordFollowupMessageBuilder()
37-
.WithContent("Sorry, I can't read the details for that server because I'm not in it or it doesn't exist!")
38-
.AsEphemeral(ephemeral: ctx.Interaction.ShouldUseEphemeralResponse(false)));
39-
return;
40-
}
41-
catch (FormatException)
42-
{
43-
await ctx.FollowupAsync(new DiscordFollowupMessageBuilder()
44-
.WithContent("That doesn't look like a valid server ID! Please try again.")
45-
.AsEphemeral(ephemeral: ctx.Interaction.ShouldUseEphemeralResponse(false)));
46-
return;
47-
}
48-
}
49-
50-
var description = "None";
51-
52-
if (guild.Description is not null) description = guild.Description;
53-
54-
var createdAt = $"{guild.Id.ToUnixTimeSeconds()}";
55-
56-
var categoryCount = guild.Channels.Count(channel => channel.Value.Type == DiscordChannelType.Category);
57-
58-
var embed = new DiscordEmbedBuilder()
59-
.WithColor(Setup.Constants.BotColor)
60-
.AddField("Server Owner", $"{(await guild.GetGuildOwnerAsync()).GetFullUsername()}")
61-
.AddField("Description", $"{description}")
62-
.AddField("Created on", $"<t:{createdAt}:F> (<t:{createdAt}:R>)")
63-
.AddField("Channels", $"{guild.Channels.Count - categoryCount}", true)
64-
.AddField("Categories", $"{categoryCount}", true)
65-
.AddField("Roles", $"{guild.Roles.Count}", true)
66-
.AddField("Members", $"{guild.MemberCount}", true)
67-
.WithThumbnail($"{guild.IconUrl}")
68-
.WithFooter($"Server ID: {guild.Id}");
69-
70-
await ctx.FollowupAsync(new DiscordFollowupMessageBuilder()
71-
.WithContent($"Server Info for **{guild.Name}**")
72-
.AddEmbed(embed)
13+
await ctx.FollowupAsync(new DiscordFollowupMessageBuilder(await ctx.Guild.CreateInfoMessageAsync())
7314
.AsEphemeral(ephemeral: ctx.Interaction.ShouldUseEphemeralResponse(false)));
7415
}
7516
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace MechanicalMilkshake.Extensions;
2+
3+
internal static class DiscordGuildExtensions
4+
{
5+
extension(DiscordGuild guild)
6+
{
7+
internal async Task<DiscordMessageBuilder> CreateInfoMessageAsync()
8+
{
9+
var message = new DiscordMessageBuilder().WithContent($"Server Info for **{guild.Name}**");
10+
11+
var description = "None";
12+
if (guild.Description is not null)
13+
description = guild.Description;
14+
15+
var createdAt = $"{guild.Id.ToUnixTimeSeconds()}";
16+
17+
var categoryCount = guild.Channels.Count(channel => channel.Value.Type == DiscordChannelType.Category);
18+
19+
message.AddEmbed(new DiscordEmbedBuilder()
20+
.WithColor(Setup.Constants.BotColor)
21+
.AddField("Server Owner", $"{(await guild.GetGuildOwnerAsync()).GetFullUsername()}")
22+
.AddField("Description", $"{description}")
23+
.AddField("Created on", $"<t:{createdAt}:F> (<t:{createdAt}:R>)")
24+
.AddField("Channels", $"{guild.Channels.Count - categoryCount}", true)
25+
.AddField("Categories", $"{categoryCount}", true)
26+
.AddField("Roles", $"{guild.Roles.Count}", true)
27+
.AddField("Members", $"{guild.MemberCount}", true)
28+
.WithThumbnail($"{guild.IconUrl}")
29+
.WithFooter($"Server ID: {guild.Id}"));
30+
31+
return message;
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)