如何让我的命令在 Discord.Net 中工作

时间:2021-06-01 21:53:24

标签: c# discord discord.net

所以我想我知道可能是什么问题,但我不确定如何解决它。我对 C# 编码很陌生。我已经在 node 中编写 Discord 机器人一年多了,所以切换有点困难。我正在遵循 Discord.NET 文档和指南中的说明。这是程序文件中的代码

using System;
using System.Threading.Tasks;
using Discord;
using Discord.WebSocket;

namespace GalacticBot
{
    class MainClass
    {
        public static void Main(string[] args) => new MainClass().MainAsync().GetAwaiter().GetResult();

        private DiscordSocketClient client;

        // Calls the class holding information
        private Config config = new Config();

        public async Task MainAsync()
        {
            client = new DiscordSocketClient();

            // Logs to console
            client.Log += Log;

            // Uses the token to start the bot
            await client.LoginAsync(TokenType.Bot, config.TestToken);
            await client.StartAsync();

            await Task.Delay(-1);
        }

        private Task Log(LogMessage msg)
        {
            Console.WriteLine(msg.ToString());
            return Task.CompletedTask;
        }
    }
}

这是CommandHandler文件中的代码

using System;
using System.Reflection;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.WebSocket;

namespace GalacticBot
{
    public class CommandHandler
    {
        private readonly DiscordSocketClient client;
        private readonly CommandService commands;

        private readonly Config config = new Config();

        public CommandHandler(DiscordSocketClient _client, CommandService _commands)
        {
            client = _client;
            commands = _commands;
        }

        public async Task InstallCommandsAsync()
        {
            client.MessageReceived += HandleCommandAsync;

            await commands.AddModulesAsync(assembly: Assembly.GetEntryAssembly(), services: null);
        }

        private async Task HandleCommandAsync(SocketMessage MessageParam)
        {
            var message = MessageParam as SocketUserMessage;
            if (message == null) return;

            int ArgPos = 0;

            // If there's no prefix or the message is from a bot then nothing happens
            if (!(message.HasCharPrefix('!', ref ArgPos) || message.HasMentionPrefix(client.CurrentUser, ref ArgPos)) || message.Author.IsBot) return;

            var context = new SocketCommandContext(client, message);

            await commands.ExecuteAsync(
                context: context,
                argPos: ArgPos,
                services: null
                );
        }
    }
}

这是命令本身的代码

using System;
using System.Threading.Tasks;
using Discord.Commands;

public class Hi : ModuleBase<SocketCommandContext>
{
    [Command("hey")]
    [Summary("Just says hi.")]

    public async Task SayAsync()
    {
        Console.WriteLine("Command used");
        await Context.Channel.SendMessageAsync("Just saying hi!");
    }
}

命令中的 Console.WriteLine 用于测试目的,看看它是否正在尝试工作。我的想法是我不会在任何地方调用和使用 CommandHandler 类。我不知道这是否是问题所在,如果是,我不知道我需要做什么。

1 个答案:

答案 0 :(得分:0)

经过更多的谷歌搜索并尝试了许多不同的想法,我终于找到了解决方案。 Anu6is 是正确的,您必须创建一个新实例并调用 install 方法。这导致在启动机器人时返回 null。首先,我为命令创建了一个变量,并使用 _client 变量将其设为静态,并为 CommandHandler 创建了一个变量

private static DiscordSocketClient _client;
private static CommandService _commands;

private CommandHandler commandHandler;

在 MainAsync 中,我必须创建一个新的 CommandService 实例,一个传递 _client 和 _commands 的命令处理程序的新实例,然后调用 InstallCommandsAsync 方法

_commands = new CommandService();

commandHandler = new CommandHandler(_client, _commands);
await commandHandler.InstallCommandsAsync();

更改后,构建成功返回,没有警告,并且测试命令有效。希望这能在某些时候对其他人有所帮助。

相关问题