【问题标题】:How to check for a certain channel /role by a certain user如何检查某个用户的某个频道/角色
【发布时间】:2019-11-29 12:01:27
【问题描述】:

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

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

那么

机器人将返回带有消息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. Bot 找到频道
  2. Bot 继续扮演所有渠道和角色。

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

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

【问题讨论】:

  • "...我唯一的限制是我不能引用客户端变量。"你在代码 sn-p 中使用client
  • 在启动通道/角色的客户端中,而不是客户端在引用bot.login 和机器人在index.js 文件中执行的其他操作时。这意味着我不能以 client 开头,因为它是未定义的,但 chane.client 不是。

标签: javascript discord.js commando


【解决方案1】:

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

【讨论】:

  • 现在我的代码只检查名称。它忽略了client.user.id
  • @SomePerson 确保您拥有创建频道的客户端的正确 ID。它在两个谓词函数中进行比较,因此不能忽略。
  • 我定义。确保存在正确的 id,由于某种原因,它仍然基于频道名称。
  • @SomePerson 日志myChannels.first().client.
  • 好吧,由于某种原因,.first() 不起作用,所以我做了myChannels.client,它返回了机器人的信息,即使我是频道的创建者。
猜你喜欢
  • 1970-01-01
  • 2020-10-30
  • 1970-01-01
  • 2013-08-07
  • 2021-03-02
  • 2022-01-03
  • 1970-01-01
  • 2021-04-11
  • 1970-01-01
相关资源
最近更新 更多