'Why my !ban command dosen't work (User not found) DISCORD.NET / C#
I'm trying to make a bot using Discord.NET in C#. If I try to ban a user with my command, an error is returned on my console app : User not found
My other commands are working
My Task HandleCommandAsync :
public async Task HandleCommandAsync(SocketMessage socketMessage)
{
var message = socketMessage as SocketUserMessage;
var context = new SocketCommandContext(_client, message);
if (message.Author.IsBot)
return;
int caracterPos = 0;
if(message.HasStringPrefix("!", ref caracterPos))
{
var result = await _commands.ExecuteAsync(context, caracterPos, _services);
if (!result.IsSuccess)
Console.WriteLine(result.ErrorReason);
if (result.Error.Equals(CommandError.UnmetPrecondition))
await message.Channel.SendMessageAsync(result.ErrorReason);
}
}
result.ErrorReason => "User not found"
await message.Channel.SendMessageAsync(result.ErrorReason); => don't send a message
My command in my commands.cs class :
[Command("ban")]
[RequireUserPermission(GuildPermission.BanMembers, ErrorMessage = "Tu n'as pas la permission de bannir des membres !")]
public async Task BanMember(SocketGuildUser user = null, [Remainder] string reason = null)
{
if (user == null)
{
await ReplyAsync("Aucun Utilisateur spécifié");
return;
}
if (reason == null)
reason = "Raison non-spécifiée";
var embedBuilder = new EmbedBuilder()
.WithDescription($":white_check_mark: {user.Mention} a été banni\n**Raison :** {reason}")
.WithColor(new Color(255,0,0));
Embed embed = embedBuilder.Build();
await ReplyAsync(embed: embed);
await Context.Guild.AddBanAsync(user, 0, reason);
}
My command in Discord : !ban @user reason
Thanks for your help !
LPR
Solution 1:[1]
Solution 2:[2]
config = new DiscordSocketConfig
{
AlwaysDownloadUsers = true,
MessageCacheSize = 100,
GatewayIntents =
GatewayIntents.Guilds |
GatewayIntents.GuildMembers |
GatewayIntents.GuildMessageReactions |
GatewayIntents.GuildMessages |
GatewayIntents.GuildVoiceStates
};
DiscordSocketClient client = new DiscordSocketClient(config);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dmitry Pavliv |
| Solution 2 | user18494041 |

