简体   繁体   English

discord js,让提到的用户耳聋

[英]discord js, deafen a mentioned user

I'm probably misunderstanding exactly what's being returned by the members object here, but I'm attempting to test a deafen command within my bot - the documentation for discord.js states that the Message object contains a Mentions property that holds all the valid mentions within a sent message, from there, I can drill down a bit further and get to the Members property from within Mentions - which returns a collection of GuildMembers that have been mentioned in the message - which is what I want.我可能误解了members object 在这里返回的确切内容,但我试图在我的机器人中测试一个聋命令 - deafen的文档指出Message ZA8CFDE6331BD59EB266 包含一个Mentions的属性,其中包含一个 Mentions 提及的 B4发送消息,从那里,我可以进一步深入研究并从Mentions中获取Members属性——它返回消息中提到的GuildMembers的集合——这就是我想要的。 The problems arises when I attempt to grab those GuildMember objects from within the collection that I get back - I'm not quite sure what I'm doing wrong, but I get back errors.当我试图从我返回的集合中获取那些GuildMember对象时,问题就出现了——我不太确定我做错了什么,但我得到了错误。 The code I have is as follows:我的代码如下:

module.exports = {
    name: 'deafen',
    cooldown: 5,
    description: 'Deafens mentioned user or users.',
    args: true,
    execute(message) {
        const taggedMembers = message.mentions.members;

        for(const member of taggedMembers) {
            member.setDeaf(true)
                .then(() => console.log(`Deafened ${member.displayName}`))
                .catch(console.error);
        }
    },
};

and I get back this error in my terminal window:我在我的终端 window 中返回了这个错误:

TypeError: member.setDeaf is not a function 
    at Object.execute (/home/tai/dev/FutabaBot/commands/admin/deafen.js:10:20)
    at Client.<anonymous> (/home/tai/dev/FutabaBot/FutabaBot.js:80:17)
    at Client.emit (events.js:210:5)
    at MessageCreateHandler.handle (/home/tai/dev/FutabaBot/node_modules/discord.js/src/client/websocket/packets/handlers/MessageCreate.js:9:34)     
    at WebSocketPacketManager.handle (/home/tai/dev/FutabaBot/node_modules/discord.js/src/client/websocket/packets/WebSocketPacketManager.js:105:65) 
    at WebSocketConnection.onPacket (/home/tai/dev/FutabaBot/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (/home/tai/dev/FutabaBot/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:296:17)
    at WebSocket.onMessage (/home/tai/dev/FutabaBot/node_modules/ws/lib/event-target.js:120:16)
    at WebSocket.emit (events.js:210:5)
    at Receiver.receiverOnMessage (/home/tai/dev/FutabaBot/node_modules/ws/lib/websocket.js:789:20)

I'm not sure if it's necessary, but I can post the other pieces of code that relate to this - and the documentation for discord.js can be found here我不确定是否有必要,但我可以发布与此相关的其他代码 - 并且可以在此处找到 discord.js 的文档

The message.mentions.members returns a Collection of GuildMembers and a Snowflake (some unique identifier . A Collection is an extension of the Map class , where a map is an array with each item having a key and a value attribute. message.mentions.members返回GuildMembers 的 Collection 和 Snowflake (一些唯一标识符。 Collection 是Map class的扩展,其中 Z1D78DC8ED51214E518B5114FE24490 的每个项目都有一个 key 和 aAEZ 值属性。

When looping over a map, like you're doing with for(const member of taggedMembers) , each item (or member in your case) is a key and a value pair.当循环一个 map 时,就像你正在做的那样for(const member of taggedMembers) ,每个项目(或你的情况下的member )都是一个键和一个值对。 Then you're trying to call the setDeaf method on that pair, which as you've seen doesn't work.然后,您尝试在该对上调用setDeaf方法,正如您所见,该方法不起作用。

What you want is to only loop over the values in the collection.你想要的是只循环集合中的值。 This can easily be done with a .forEach .这可以通过.forEach轻松完成。 I'll add some example code down below:我将在下面添加一些示例代码:

taggedMembers.forEach((member) => {
    member.setDeaf(true)
        .then(() => console.log(`Deafened ${member.displayName}`))
        .catch(console.error);
});

Give it a go and let me know if you run into any more issues.给它一个 go 如果您遇到任何问题,请告诉我。

EDIT: As slothiful pointed out in the comments, using a .forEach won't catch any Promise rejections even after attaching a .catch() .编辑:正如懒惰在评论中指出的那样,即使附加了.catch() ,使用.forEach也不会捕获任何 Promise 拒绝。 To fix this you can use the for... of loop but destructuring the pair.要解决此问题,您可以使用for... of循环但解构该对。 This can be seen in the example below:这可以在下面的示例中看到:

for (let [, member] of taggedMembers) {
    member.setDeaf(true)
        .then(() => console.log(`Deafened ${member.displayName}`))
        .catch(console.error);
}

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

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