如何通过某个用户检查某个频道/角色
How to check for a certain channel /role by a certain user
我希望我的机器人能够检查...
- 如果我的机器人创建的频道与某个名称匹配...
或者
- 如果我的机器人创建的角色匹配某个名称...
然后
机器人将 return 带有消息 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
变量,因为它 return 对我来说是未定义的,但是像 channel.client
这样的东西是允许的,因为它定义了消息发送的频道的创建者。
哪里出了问题
这是出了什么问题。
- 机器人没有找到频道频道
- 机器人 returns
或
- 机器人找到频道
- Bot继续制作所有频道和角色。
我预计:Bot 可以在公会中搜索满足 client.id 和名称预期的频道,然后 return。此外,Bot 可以在公会中搜索符合所有者(我的(指我是机器人))id 和名称的角色,然后 return 也。
实际结果是机器人 return在不应该的时候出现,例如Bot returns 在它应该创建一个频道时找不到频道。 (虽然我删掉了它的代码)
Collection.filter()
将始终 return 一个新的 Collection。因此,您的第一个条件总是 return 为真并执行 return
语句,因为变量存在。
要么检查 size
property of the Collection, or use Collection.find()
如果谓词函数 return 为真,它只会 return 一个元素。
此外,您必须通过审核日志来检查频道的创建者。实例化器是创建object实例的客户端,这并不等同于实际创建通道本身。
// 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);
}
我希望我的机器人能够检查...
- 如果我的机器人创建的频道与某个名称匹配... 或者
- 如果我的机器人创建的角色匹配某个名称...
然后
机器人将 return 带有消息 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
变量,因为它 return 对我来说是未定义的,但是像 channel.client
这样的东西是允许的,因为它定义了消息发送的频道的创建者。
哪里出了问题
这是出了什么问题。
- 机器人没有找到频道频道
- 机器人 returns
或
- 机器人找到频道
- Bot继续制作所有频道和角色。
我预计:Bot 可以在公会中搜索满足 client.id 和名称预期的频道,然后 return。此外,Bot 可以在公会中搜索符合所有者(我的(指我是机器人))id 和名称的角色,然后 return 也。
实际结果是机器人 return在不应该的时候出现,例如Bot returns 在它应该创建一个频道时找不到频道。 (虽然我删掉了它的代码)
Collection.filter()
将始终 return 一个新的 Collection。因此,您的第一个条件总是 return 为真并执行 return
语句,因为变量存在。
要么检查 size
property of the Collection, or use Collection.find()
如果谓词函数 return 为真,它只会 return 一个元素。
此外,您必须通过审核日志来检查频道的创建者。实例化器是创建object实例的客户端,这并不等同于实际创建通道本身。
// 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);
}