C# - 当某些短语被说出时(不是前缀命令),有不和谐的机器人执行功能

C# - 当某些短语被说出时(不是前缀命令),有不和谐的机器人执行功能

问题描述:

所以我完成了大部分我想为我的不和机器人实现的命令。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/),并且在没有用户请求的情况下加载网页并打印出网页中的某些元素。

目前你有下面的代码行

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 
}