简体   繁体   English

如何检查某个用户的某个频道/角色

[英]How to check for a certain channel /role by a certain user

I want my bot to be able to check... 我希望我的机器人能够检查...

  • If a channel my bot made matches a certain name... OR 如果我的漫游器创建的频道与某个名称匹配...或者
  • If a role my bot made matches a certain name... 如果我的机器人所扮演的角色与某个名称匹配...

THEN 然后

The bot would return the action with the message Mod mail is already activated here! 机器人将返回操作,并显示消息Mod mail is already activated here!

Here's my code 这是我的代码

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!`)

NOTE 注意

I am doing in this in discord.js-commando , not discord.js so this is not in my index.js file so my only limitation is I can't refer to the client variable. 我是在discord.js-commando中而不是discord.js中这样做的,所以这不在我的index.js文件中,所以我唯一的限制是我不能引用客户端变量。 Directory is ~/commands/extras/modmail.js/ instead of ~/index.js/ What I mean by this is I cannot start with the client variable because it will return to me as undefined, but something like channel.client would be allowed because it's defining the creator of the channel the messgae was sent in. 目录是〜/ commands / extras / modmail.js /而不是〜/ index.js /我的意思是我不能以client变量开头,因为它将以未定义的形式返回给我,但是类似channel.client允许,因为它定义了发送信息的渠道的创建者。

What went wrong 什么地方出了错

Here's what went wrong. 这是哪里出了问题。

  1. Bot doesn't find the channel channel Bot找不到频道
  2. Bot returns 机器人退货

OR 要么

  1. Bot finds channel Bot找到频道
  2. Bot continues to make all the channels and roles. Bot继续扮演所有渠道和角色。

I expect: Bot can search the guild for a channel that meets client.id AND name expectations and then return. 我期望:Bot可以在行会中搜索符合client.id并满足期望名称的频道,然后返回。 Also, Bot can search the guild for a role that meets the owner (my(Referring to me being the bot)) id AND name and then return also. 此外,Bot可以在行会中搜索符合所有者(我(指我是机器人)的角色)ID和名称的角色,然后也返回。

The actual result is the bot returning when it's not supposed to, eg Bot returns when it doesn't find a channel when it is supposed to create one. 实际结果是漫游器在不应该返回的时候返回,例如,机器人在应该创建一个通道的时候未找到通道时返回。 (I cut out the code for it though) (虽然我剪掉了代码)

Collection.filter() will always return a new Collection. Collection.filter()将始终返回新的Collection。 Therefore, your first condition is always returning true and executing the return statement because the variable exists. 因此,由于变量存在,因此您的第一个条件始终是返回true并执行return语句。

Either check the size property of the Collection, or use Collection.find() which will only return an element if the predicate function returns true. 检查Collection的size属性,或使用Collection.find() ,该函数仅在谓词函数返回true时才返回元素。

Also, you'll have to go through the Audit Logs to check the creator of a channel. 另外,您还必须通过审核日志来检查频道的创建者 The instantiator is the client that created the instance of the object , which is not equivalent to actually creating the channel itself. 实例化程序是创建对象实例的客户端,这并不等同于实际创建通道本身。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM