(Discord bot 1.0 c#)如何创建新类并添加CMD?
你好!(第一次在这里发帖,希望大家友好:P) 因此,我决定在c#中制作一个discord bot 1.0(我正在学习c#atm),我遇到了一个问题,我不知道如何解决它 所以,来描述我要做的是以下内容 我正在尝试使它成为一个可以为x命令(如.say等)创建不同类的类,而不是将它们都放在下面的“命令”中,这样就更容易使用了 我得到了这三个剧本,但第四个剧本没法发挥作用 //启动(Discord bot 1.0 c#)如何创建新类并添加CMD?,c#,discord,discord.net,C#,Discord,Discord.net,你好!(第一次在这里发帖,希望大家友好:P) 因此,我决定在c#中制作一个discord bot 1.0(我正在学习c#atm),我遇到了一个问题,我不知道如何解决它 所以,来描述我要做的是以下内容 我正在尝试使它成为一个可以为x命令(如.say等)创建不同类的类,而不是将它们都放在下面的“命令”中,这样就更容易使用了 我得到了这三个剧本,但第四个剧本没法发挥作用 //启动 using System; using System.Threading.Tasks; using Discord; us
using System;
using System.Threading.Tasks;
using Discord;
using Discord.WebSocket;
using Discord.Commands;
namespace MyBot
{
public class Program
{
// Convert our sync main to an async main.
public static void Main(string[] args) =>
new Program().Start().GetAwaiter().GetResult();
private DiscordSocketClient client;
private CommandHandler handler;
public async Task Start()
{
// Define the DiscordSocketClient
client = new DiscordSocketClient();
var token = "Censored";
// Login and connect to Discord.
await client.LoginAsync(TokenType.Bot, token);
await client.StartAsync();
var map = new DependencyMap();
map.Add(client);
handler = new CommandHandler();
await handler.Install(map);
// Block this program until it is closed.
await Task.Delay(-1);
}
private Task Log(LogMessage msg)
{
Console.WriteLine(msg.ToString());
return Task.CompletedTask;
}
}
}
//我的命令处理程序
using System.Threading.Tasks;
using System.Reflection;
using Discord.Commands;
using Discord.WebSocket;
namespace MyBot
{
public class CommandHandler
{
private CommandService commands;
private DiscordSocketClient client;
private IDependencyMap map;
public async Task Install(IDependencyMap _map)
{
// Create Command Service, inject it into Dependency Map
client = _map.Get<DiscordSocketClient>();
commands = new CommandService();
_map.Add(commands);
map = _map;
await commands.AddModulesAsync(Assembly.GetEntryAssembly());
client.MessageReceived += HandleCommand;
}
public async Task HandleCommand(SocketMessage parameterMessage)
{
// Don't handle the command if it is a system message
var message = parameterMessage as SocketUserMessage;
if (message == null) return;
// Mark where the prefix ends and the command begins
int argPos = 0;
// Determine if the message has a valid prefix, adjust argPos
if (!(message.HasMentionPrefix(client.CurrentUser, ref argPos) || message.HasCharPrefix('!', ref argPos))) return;
// Create a Command Context
var context = new CommandContext(client, message);
// Execute the Command, store the result
var result = await commands.ExecuteAsync(context, argPos, map);
// If the command failed, notify the user
if (!result.IsSuccess)
await message.Channel.SendMessageAsync($"**Error:** {result.ErrorReason}");
}
}
}
如果你知道我做错了什么,请告诉我或告诉我一些关于这个问题的信息,我可以尝试解决:)
提前谢谢!
PS,如果这个问题有重复,我很抱歉,但我不知道要搜索什么才能找到它
编辑
有人告诉我“在课堂上安装metohds(cmds)”,但我该怎么做呢?答案如下 在类{name}之前添加Public,以便
namespace MyBot.Modules.Public
{
**Public** class test : ModuleBase
{
[Command("say")]
[Alias("echo")]
[Summary("Echos the provided input")]
public async Task Say([Remainder] string input)
{
await ReplyAsync(input);
}
}
}答案如下 在类{name}之前添加Public,以便
namespace MyBot.Modules.Public
{
**Public** class test : ModuleBase
{
[Command("say")]
[Alias("echo")]
[Summary("Echos the provided input")]
public async Task Say([Remainder] string input)
{
await ReplyAsync(input);
}
}
}
namespace MyBot.Modules.Public
{
**Public** class test : ModuleBase
{
[Command("say")]
[Alias("echo")]
[Summary("Echos the provided input")]
public async Task Say([Remainder] string input)
{
await ReplyAsync(input);
}
}