C
C#•3y 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•3y ago
can i see the full block of code? like the function itself + its attributes?
REAPER
REAPEROP•3y ago
pip
pip•3y 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
REAPEROP•3y ago
Thanks a lot 😄
pip
pip•3y 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
REAPEROP•3y 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•3y 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
REAPEROP•3y ago
Ok thanks a lot rooHappy
Accord
Accord•3y ago
✅ This post has been marked as answered!
REAPER
REAPEROP•3y 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•3y ago
maybe try var roles = (Context.Interaction.User as SocketGuildUser).Roles;
REAPER
REAPEROP•3y ago
That works 🤩
pip
pip•3y ago
Sweet 😎
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server