0

我希望我的机器人能够检查...

  • 如果我的机器人制作的频道与某个名称匹配......或者
  • 如果我的机器人创建的角色与某个名称匹配...

然后

机器人将返回带有消息的操作Mod mail is already activated here!

这是我的代码

var mg = message.guild

const mythings = mg.channels.filter(chane => chane.client.user.id === `595840576386236437` && chane.name === 'modmail' &&chane.type === `text`);
const mythings2 = mg.channels.filter(chane => chane.client.user.id === `595840576386236437` && chane.name === 'qanda' &&chane.type === `text`);
const mythings3 = mg.roles.filter(role => role.client.user.id === `595840576386236437` && role.name === 'ModMail');

console.log(mythings)

if(mythings)  return message.channel.send(`Rejected! There's possible fragments of modmail already set up here!`)
if(mythings2) return message.channel.send(`Rejected! There's possible fragments of modmail already set up here!`)
if(!mythings3) return message.channel.send(`Rejected! There's possible fragments of modmail already set up here!`)

笔记

我在discord.js-commando中做这件事,而不是discord.js所以这不在我的 index.js 文件中,所以我唯一的限制是我不能引用客户端变量。目录是~/commands/extras/modmail.js/而不是~/index.js/我的意思是我不能从client变量开始,因为它会以未定义的形式返回给我,但是channel.client因为它正在定义,所以允许使用类似的东西发送消息的频道的创建者。

什么地方出了错

这就是问题所在。

  1. Bot 找不到频道频道
  2. 机器人返回

或者

  1. 机器人找到频道
  2. Bot 继续制作所有渠道和角色。

我期望:机器人可以在公会中搜索满足 client.id AND name 期望的频道,然后返回。此外,Bot 可以在公会中搜索满足所有者(我的(指我是机器人))id AND name 的角色,然后也返回。

实际结果是机器人在不应该返回时返回,例如,当它应该创建一个通道时,它没有找到一个通道时机器人返回。(虽然我删掉了它的代码)

4

1 回答 1

0

Collection.filter()将始终返回一个新的集合。因此,您的第一个条件总是返回 true 并执行return语句,因为变量存在。

要么检查size集合的属性,要么使用Collection.find()它只会在谓词函数返回 true 时返回一个元素。

此外,您必须通过审核日志来检查频道的创建者。实例化器是创建对象实例的客户端,并不等同于实际创建通道本身。

// Async context (meaning within an async function) needed to use 'await'

try {
  const channelLogs = await mg.fetchAuditLogs({ user: '595840576386236437', type: 10 });
  const myChannels = channelLogs.entries.filter(e => e.target && ['modmail', 'qanda'].includes(e.target.name) && e.target.type === 'text');

  const roleLogs = await mg.fetchAuditLogs({ user: '595840576386236437', type: 30 });
  const myRole = roleLogs.entries.find(e => e.target && e.target.name === 'Modmail');

  if (myChannels.size !== 0 || myRole) {
    return await message.channel.send('Rejected! There\'s possible fragments of modmail already set up here!');
  }
} catch(err) {
  console.error(err);
}
于 2019-07-21T20:27:11.710 回答