How can I have my Discord C# respond to mentions?

梦想的初衷 提交于 2020-01-06 07:26:32

问题


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());
        }

回答1:


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);
            }
        }
    }



回答2:


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.



来源:https://stackoverflow.com/questions/50052214/how-can-i-have-my-discord-c-sharp-respond-to-mentions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!