所以我已经完成了我想要为我的discord bot实现的大多数命令。
< [组(“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/),并且无需用户请求,加载页面并从所述网页打印出某些元素。< / p>
答案 0 :(得分:0)
目前您拥有以下代码行
if (!(message.HasCharPrefix('$', ref argPos) || message.HasMentionPrefix(client.CurrentUser, ref argPos))) return;
如果条件不匹配,Return;
会使命令处理停止。在这种情况下,前缀或机器人提及检查。
您可以展开这段代码,首先执行一段代码,然后再确定它不需要执行任何操作。
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
}