2017-08-16 105 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