I'm in the middle of developing a C# discord bot and I have been successful with making the bot respond to phrases starting with "c! " and responding with the correct command, however I want the bot to reply with a GIF if the bot is mentioned. If someone could help explain why this does not work and how to fix it that would be nice. This is my code right now:
private async Task HandleCommandAsync(SocketMessage arg)
{
var message = arg as SocketUserMessage;
if (message is null || message.Author.IsBot) return;
int argPos = 0;
if (message.HasStringPrefix("", ref argPos))
{
var context = new SocketCommandContext(_client, message);
var result = await _commands.ExecuteAsync(context, argPos, _services);
if (!result.IsSuccess)
Console.WriteLine(result.ErrorReason);
}
if (message.HasMentionPrefix(_client.CurrentUser, ref argPos))
{
var embed = new EmbedBuilder();
embed.WithImageUrl("https://cdn.discordapp.com/attachments/138522037181349888/438774275546152960/Ping_Discordapp_GIF-downsized_large.gif");
await ReplyAsync("", false, embed.Build());
}
It may be better to create a Modules folder and add your commands in a separate .cs file. To do that, you need to add this line of code after you initiate your _client but before you call LoginAsync.
_client.MessageReceived += HandleCommandAsync;
await _commands.AddModulesAsync(Assembly.GetEntryAssembly());
Then, create a seperate new class. Your command can accept a SocketGuildUser as a parameter. In chat type !help @name to call the command:
public class Help : ModuleBase<SocketCommandContext>
{
[Command("help")]
public async Task HelpAsync(SocketGuildUser user)
{
await ReplyAsync($"{user.Mention} needs a lot of help!");
}
}
Your HandleCommandAsync should look like this:
private async Task HandleCommandAsync(SocketMessage arg)
{
var message = arg as SocketUserMessage;
if (message == null || message.Author.IsBot) return;
int argPos = 0;
if (message.HasStringPrefix("!", ref argPos) ||
message.HasMentionPrefix(_client.CurrentUser, ref argPos))
{
var context = new SocketCommandContext(_client, message);
var result = await _commands.ExecuteAsync(context, argPos, _services);
if (!result.IsSuccess)
{
Console.WriteLine(result.ErrorReason);
}
}
}
I agree with what Solarcloud said about seperating your modules but this is all you had to modify your code into:
private async Task HandleCommandAsync(SocketMessage arg)
{
var message = arg as SocketUserMessage;
if (message is null || message.Author.IsBot) return;
int argPos = 0;
if (message.HasStringPrefix("", ref argPos))
{
var context = new SocketCommandContext(_client, message);
var result = await _commands.ExecuteAsync(context, argPos, _services);
if (!result.IsSuccess)
Console.WriteLine(result.ErrorReason);
}
if (message.content.contains(_client.CurrentUser.Mention.Replace("!", "")))
{
var embed = new EmbedBuilder();
embed.WithImageUrl("https://cdn.discordapp.com/attachments/138522037181349888/438774275546152960/Ping_Discordapp_GIF-downsized_large.gif");
await ReplyAsync("", false, embed.Build());
}
The replace is just in case your bot's ID returns @<!123456789>
, getting rid of it works properly.