C
C#β€’2y ago
REAPER

Slash Command purge [Answered]

Hi, I'm trying to use my old purge command as a slash command, but I end up giving me the Discord Error "Application Didn't Respond". I've tried making the var message a RespondAsync but the code is not happy about that rooCry Does anyone know how to solve this issue of mine?
var messages = await Context.Interaction.Channel.GetMessagesAsync(amount).FlattenAsync();
await (Context.Interaction.Channel as SocketTextChannel).DeleteMessagesAsync(messages);

var message = await Context.Interaction.Channel.SendMessageAsync($"{messages.Count()} messages deleted successfully!");
await Task.Delay(4000);
await message.DeleteAsync();
var messages = await Context.Interaction.Channel.GetMessagesAsync(amount).FlattenAsync();
await (Context.Interaction.Channel as SocketTextChannel).DeleteMessagesAsync(messages);

var message = await Context.Interaction.Channel.SendMessageAsync($"{messages.Count()} messages deleted successfully!");
await Task.Delay(4000);
await message.DeleteAsync();
14 Replies
pip
pipβ€’2y ago
can i see the full block of code? like the function itself + its attributes?
REAPER
REAPERβ€’2y ago
pip
pipβ€’2y ago
So important note is that an interaction response does not return anything, but i'll show you how to get around that one sec
REAPER
REAPERβ€’2y ago
Thanks a lot πŸ˜„
pip
pipβ€’2y ago
var messages = await Context.Interaction.Channel.GetMessagesAsync(amount).FlattenAsync();
await (Context.Interaction.Channel as SocketTextChannel).DeleteMessagesAsync(messages);

await Context.Interaction.RespondAsync($"{messages.Count()} messages deleted successfully!", ephemeral: true);
var messages = await Context.Interaction.Channel.GetMessagesAsync(amount).FlattenAsync();
await (Context.Interaction.Channel as SocketTextChannel).DeleteMessagesAsync(messages);

await Context.Interaction.RespondAsync($"{messages.Count()} messages deleted successfully!", ephemeral: true);
you can make the message itself ephemeral so that it automatically deletes itself or doesn't show up basically paste that into your code and see if that works
REAPER
REAPERβ€’2y ago
Wow thanks a lot didn't know that 🀯 Oh yeah now that you're here. Is there a smart way of making error messages if a user is trying to use a slash command they don't have the required role for?
pip
pipβ€’2y ago
i personally dont use the RequireRole attributes and that stuff, so I'm not sure if Discord.Net / DSharpPlus has something built in that you can overwrite but how i do it is something like this
var roles = Context.Interaction.User.Roles;
if(roles.Any(p => p.Name == "your role name here") == true)
{
await Context.Interaction.RespondAsync("This is your custom error message!");
return;
}
//the rest of your logic here
var roles = Context.Interaction.User.Roles;
if(roles.Any(p => p.Name == "your role name here") == true)
{
await Context.Interaction.RespondAsync("This is your custom error message!");
return;
}
//the rest of your logic here
REAPER
REAPERβ€’2y ago
Ok thanks a lot rooHappy
Accord
Accordβ€’2y ago
βœ… This post has been marked as answered!
REAPER
REAPERβ€’2y ago
Just tried it but it can't find Roles in Context.Interaction.User.Roles.?? It says: 'SocketUser' does not contain a definition for 'Roles' and no accessible extension method 'Roles' accepting a first argument of type 'SocketUser' could be found (are you missing a using directive or an assembly reference?)
pip
pipβ€’2y ago
maybe try var roles = (Context.Interaction.User as SocketGuildUser).Roles;
REAPER
REAPERβ€’2y ago
That works 🀩
pip
pipβ€’2y ago
Sweet 😎
Unknown User
Unknown Userβ€’2y ago
Message Not Public
Sign In & Join Server To View