我一直在试图从0.9.6 Discord.NET API交换了我的代码到新的1.0.1 API,它的基本要求一个完整的重组我码。但是我首先遇到了一些麻烦,实际上让机器人启动并运行。不和谐机器人运行,但将无法连接到服务器
我设置代码机构按指南链接here
虽然它运行没有错误,机器人本身是不是在网上我的服务器出现。
你提问之前,我实际上已经取代了“博特令牌这里”与实际机器人令牌。
namespace DiscordBot{
public class Program
{
private CommandService commands;
private DiscordSocketClient client;
private IServiceProvider services;
static void Main(string[] args) => new Program().Start().GetAwaiter().GetResult();
public async Task Start()
{
client = new DiscordSocketClient();
commands = new CommandService();
string token = "<token>";
services = new ServiceCollection()
.BuildServiceProvider();
await InstallCommands();
await client.LoginAsync(TokenType.Bot, token);
await client.StartAsync();
await Task.Delay(-1);
}
public async Task InstallCommands()
{
// Hook the MessageReceived Event into our Command Handler
client.MessageReceived += HandleCommand;
// Discover all of the commands in this assembly and load them.
await commands.AddModulesAsync(Assembly.GetEntryAssembly());
}
public async Task HandleCommand(SocketMessage messageParam)
{
// Don't process the command if it was a System Message
var message = messageParam as SocketUserMessage;
if (message == null) return;
// Create a number to track where the prefix ends and the command begins
int argPos = 0;
// Determine if the message is a command, based on if it starts with '!' or a mention prefix
if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(client.CurrentUser, ref argPos))) return;
// Create a Command Context
var context = new CommandContext(client, message);
// Execute the command. (result does not indicate a return value,
// rather an object stating if the command executed successfully)
var result = await commands.ExecuteAsync(context, argPos, services);
if (!result.IsSuccess)
await context.Channel.SendMessageAsync(result.ErrorReason);
}
}
}
再来说MyBot.cs类
namespace DiscordBot
{
class MyBot : ModuleBase
{
private CommandService _service;
public MyBot(CommandService service)
{
_service = service;
}
}
}
感谢您的输入,结果发生了一些事情。 | | System.PlatformNotSupportedException:此平台不支持Websocket协议 经过一番研究,事实证明它与Windows 7不兼容?你知道有没有解决这个问题的方法? – John
@john是的,有。但是,这将是一个长期的评论,并会值得一个新的问题 –
好吧,谢谢你让我知道。我会发布一个新的问题,然后:) – John