2017-08-22 36 views
0

所以我完成了大部分我想爲我的不和機器人實現的命令。C# - 當某些短語被說出時(不是前綴命令),有不和諧的機器人執行功能

< [Group(「GroupName」)]>功能非常簡單易懂,更易於理解更新的Discord.NET。然而,與0.9.6版本不同,我不知道如何讓機器人執行一個功能,而無需等待前綴被註冊。 進入這裏

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); 
    } 

代碼從Foxbot引導直複製粘貼。現在我明白它在註冊命令之前要等待'$'或者@bot提及。

我想要做的是讓機器人能夠尋找特定的網頁網址(http://archiveofourown.org/),並且在沒有用戶請求的情況下加載網頁並打印出網頁中的某些元素。

回答

0

目前你有下面的代碼行

if (!(message.HasCharPrefix('$', ref argPos) || message.HasMentionPrefix(client.CurrentUser, ref argPos))) return; 

Return;使得它,如果條件不符合您的命令處理停止。在這種情況下是前綴或bot提到檢查。

您可以在確定不需要執行任何操作之前,先展開這段代碼,先執行一段代碼。

if (!(message.HasCharPrefix('$', ref argPos) || message.HasMentionPrefix(client.CurrentUser, ref argPos))) 
{ 
    if(check if there is a website) 
    { 
     // if there is a wesbite, do whatever you want to do with it 
    } 
    return; // still return here, as you don't want further command execution 
} 
相關問題