我的机器人的discord.js函数出现问题

时间:2020-04-09 03:45:55

标签: node.js discord discord.js

我想创建一个脚本来知道哪个机器人在服务器中拥有我的机器人并创建邀请,第二个我想创建一个命令向所有成员发送一条私人消息(不是非raid机器人)

  1. 脚本:

    client.on("ready", () => {
    client.user.setActivity('ESPADA');
        client.guilds.forEach(serv => {
          serv.channels.random().createInvite().then(invite => console.log(`>${serv.name} | ${invite}`))
          .catch(console.log(`>${serv.name} | Entrain d'envoyer une invite... `));
        });

  1. 和我的错误:

    (节点:10752)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性'forEach' 在Client.client.on(c:\ Users \ mathis \ Downloads \ self bot ash \ Raid.js:10:18) 在Client.emit(events.js:189:13) 在WebSocketManager.triggerClientReady(c:\ Users \ mathis \ node_modules \ discord.js \ src \ client \ websocket \ WebSocketManager.js:433:17) 在WebSocketManager.checkShardsReady(c:\ Users \ mathis \ node_modules \ discord.js \ src \ client \ websocket \ WebSocketManager.js:417:10) 在WebSocketShard.shard.on.unavailableGuilds(c:\ Users \ mathis \ node_modules \ discord.js \ src \ client \ websocket \ WebSocketManager.js:199:14) 在WebSocketShard.emit(events.js:189:13) 在WebSocketShard.checkReady(c:\ Users \ mathis \ node_modules \ discord.js \ src \ client \ websocket \ WebSocketShard.js:466:12) 在WebSocketShard.onPacket上(c:\ Users \ mathis \ node_modules \ discord.js \ src \ client \ websocket \ WebSocketShard.js:438:16) 在WebSocketShard.onMessage(c:\ Users \ mathis \ node_modules \ discord.js \ src \ client \ websocket \ WebSocketShard.js:293:10) 在WebSocket.onMessage(c:\ Users \ mathis \ node_modules \ ws \ lib \ event-target.js:120:16)

  2. 我的DM命令

    
        if(message.content === "!mp"){
            if(message.deletable) message.delete();
            i = 0;
            message.guild.members.forEach(member => { ```
    
    4. the error :
    
    TypeError: message.guild.members.forEach is not a function
        at Client.<anonymous> (c:\Users\mathis\Downloads\self bot ash\Raid.js:98:31)
        at Client.emit (events.js:194:15)
        at MessageCreateAction.handle (c:\Users\mathis\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
        at Object.module.exports [as MESSAGE_CREATE] (c:\Users\mathis\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
        at WebSocketManager.handlePacket (c:\Users\mathis\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)
        at WebSocketShard.onPacket (c:\Users\mathis\node_modules\discord.js\src\client\websocket\WebSocketShard.js:435:22)
        at WebSocketShard.onMessage (c:\Users\mathis\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
        at WebSocket.onMessage (c:\Users\mathis\node_modules\ws\lib\event-target.js:120:16)
        at WebSocket.emit (events.js:189:13)
        at Receiver.receiverOnMessage (c:\Users\mathis\node_modules\ws\lib\websocket.js:801:20)
    

感谢您的回复!

2 个答案:

答案 0 :(得分:1)

这似乎是您的discord.js版本为v12或更高版本(至少在DM命令中)的问题。
从v12开始,Client#guildsGuild#members不再返回Collection 而是返回Manager。因此,现在Guild#members返回一个GuildMemberManager,而Client#guilds返回一个GuildManager

幸运的是,仍然有一种方法可以访问相应的Manager集合,那就是通过<SomeManager>.cache
cache属性无一例外地存在于每个Manager中(因为它是BaseManager中的属性)。
因此,要访问client.guildsguild.members集合,您需要执行以下操作:

Client#guilds

// This returns the collection: Collection<Snowflake, Guild>
// which then you can use methods on such as .forEach() or .every()
client.guilds.cache; 

// Using .forEach()
client.guilds.cache.forEach(guild => console.log(guild.name));

Guild#members

// This returns the collection: Collection<Snowflake, GuildMember>
// which then you can use methods on such as .forEach() or .every()
guild.members.cache;

// Using .forEach()
guild.members.cache.forEach(member => console.log(member.displayName));

还有其他几种Manager类型,建议您咨询this来了解v12中的更改。 youtube上较旧的教程使用discord.js v11或更低版本,但我还没有看到任何更新版本。

答案 1 :(得分:0)

我的建议是使用var info= new RegionInfo("TH"); var currency = info.CurrencySymbol; 而不是every;我以前遇到过这样的问题。

P.S。如果这是一个自我机器人,请记住他们违反了Discord的terms of service