2017-08-16 35 views
1

我一直在试图从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; 
    } 
    } 
} 

回答

0

你可能想要做的第一件事是添加一些记录到你的机器人。 由于您的代码可能是正确的,但由于任何原因,不和谐可能会拒绝您的连接。

await client.StartAsync();后添加

client.Log += (msg) => {return Console.WriteLine(${msg.ToString()}");};` 

这将输出消息您从您的客户端控制台接收。

现在,你还需要配置哪些信息应被发送到该事件。这可以在创建您的DiscordClient()时完成。因此,而不是client = new DiscordSocketClient();你可以使用

client = new DiscordSocketClient(
    new DiscordSocketConfig() 
    { 
     LogLevel = LogSeverity.Verbose 
    } 
); 

放牧,应该给你你需要的所有信息。但是,您也可以使用LogSeverity.Debug,这是可用的最高日志记录,因此会为您提供所有消息。

现在,你有你的控制台一些反馈,去看看你有什么具体的错误。

此外,我建议首先完成链接的教程your first bot一部分,而不是直接步入命令。一旦你得到了这个工作,你可以继续

+0

感谢您的输入,结果发生了一些事情。 | | System.PlatformNotSupportedException:此平台不支持Websocket协议 经过一番研究,事实证明它与Windows 7不兼容?你知道有没有解决这个问题的方法? – John

+0

@john是的,有。但是,这将是一个长期的评论,并会值得一个新的问题 –

+0

好吧,谢谢你让我知道。我会发布一个新的问题,然后:) – John

相关问题