繁体   English   中英

如何解决 fn 不是函数问题?

[英]How do I solve the fn is not a function issue?

在我添加反应代码之前,它作为基本规则工作得很好。 但我想让人们不得不接受规则才能继续不和谐。 然后,我有问题,我认为部分问题是意图和部分,但我需要允许机器人启动的意图和允许代码进行反应和通道的部分,以便规则机器人可以访问其中的通道角色添加/接受规则将是。

主.js

const Discord = require('discord.js');
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES",]},{partials: ["MESSAGE", "CHANNEL", "REACTION"]});
const prefix = '-';
const fs = require('fs');

client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}

client.once('ready', () =>{
    console.log('Roleplay Is Online');
});
client.on('guildMemberAdd', guildMember =>{
    let welcomeRole = guildMember.guild.roles.cache.find('973945417991589938');
 
    guildMember.roles.add(welcomeRole);
    guildMember.guild.channels.cache.get('974611802941968447').send(`Welcome <@${guildMember.user.id}> to our server! Make sure to check out the rules channel!`)
});

client.on('message', message => {
    if(!message.content.startsWith(prefix) || message.author.bot)return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if(command === 'rules'){
        client.commands.get('rules').execute(message, args, Discord);}else if(command === 'clear'){client.commands.get('clear').execute(message, args, Discord);}else if(command === 'kick'){client.commands.get('kick').execute(message, args, Discord);}else if(command === 'ban'){client.commands.get('ban').execute(message, args, Discord);
    }else if(command === 'mute'){client.commands.get('mute').execute(message, args, Discord);};
});

client.login('****************************************');

规则.js

module.exports = {
    name: 'rules',
    description: 'rule command',
    async execute(message, args, Discord, client){
        const channel = '973944809444245504';
        const MemberRole = message.guild.roles.cache.find("973945417991589938");
 
        const MemberEmoji = ':ballot_box_with_check:';
 
        let embed = new Discord.MessageEmbed()
            .setColor('#e42643')
            .setTitle('Rules')
            .setDescription('Server Rules You Must Follow')
            .addFields(
                { name: 'Rule 1', value: 'Respect Everyone We Are All Human!', inline: false },
                { name: 'Rule 2', value: 'No Explicit Content Will Resault In Instant Ban', inline: false },
                { name: 'Rule 3', value: 'Do Not Spam @ Mods Or Owners You Will be Kicked If Repeat Offence = Ban', inline: false },
                { name: 'Rule 4', value: 'Each Channel Has Its Own Use So Use The Right One Please', inline: false },
                { name: 'Rule 5', value: 'No Trolling/Spamming This Resaults In Kick', inline: false },
                { name: 'Rule 6', value:'Any Exploiting of the Roleplay bot will resault in a character reset please report any bugs or exploits that are found Thanks.'},
                { name: 'Do You Agree', value: 'if you agree to the rules please click the tick below if you do not follow the rules you agree to will consit of punishments.'}
            )
 
        let messageEmbed = await message.channel.send(embed);
        messageEmbed.react(MemberEmoji);
 
        client.on('messageReactionAdd', async (reaction, user) => {
            if (reaction.message.partial) await reaction.message.fetch();
            if (reaction.partial) await reaction.fetch();
            if (user.bot) return;
            if (!reaction.message.guild) return;
 
            if (reaction.message.channel.id == channel) {
                if (reaction.emoji.name === MemberEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(MemberRole);
                }
            } else {
                return;
            }
 
        });
 
        client.on('messageReactionRemove', async (reaction, user) => {
 
            if (reaction.message.partial) await reaction.message.fetch();
            if (reaction.partial) await reaction.fetch();
            if (user.bot) return;
            if (!reaction.message.guild) return;
 
 
            if (reaction.message.channel.id == channel) {
                if (reaction.emoji.name === MemberEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(MemberRole);
                }
            } else {
                return;
            }
        });
    }
 
}  

运行命令-rules时出现的错误是:

Roleplay Is Online
(node:17556) DeprecationWarning: The message event is deprecated. Use messageCreate instead
(Use `node --trace-deprecation ...` to show where the warning was created)
C:\Users\*****\Desktop\TBots\Roleplay\node_modules\@discordjs\collection\dist\index.js:122
      if (fn(val, key, this))
          ^

TypeError: fn is not a function
    at Map.find (C:\Users\*****\Desktop\TBots\Roleplay\node_modules\@discordjs\collection\dist\index.js:122:11)
    at Object.execute (C:\Users\*****\Desktop\TBots\Roleplay\commands\rules.js:6:54)
    at Client.<anonymous> (C:\Users\*****\Desktop\TBots\Roleplay\main.js:31:38)
    at Client.emit (node:events:527:28)
    at MessageCreateAction.handle (C:\Users\*****\Desktop\TBots\Roleplay\node_modules\discord.js\src\client\actions\MessageCreate.js:34:18)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\*****\Desktop\TBots\Roleplay\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\*****\Desktop\TBots\Roleplay\node_modules\discord.js\src\client\websocket\WebSocketManager.js:351:31)
    at WebSocketShard.onPacket (C:\Users\*****\Desktop\TBots\Roleplay\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\******\Desktop\TBots\Roleplay\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\*******\Desktop\TBots\Roleplay\node_modules\ws\lib\event-target.js:199:18)
PS C:\Users\*******\Desktop\TBots\Roleplay> 

这是因为在message.guild.roles.cache.find("973945417991589938")你提供了一个字符串,但Collection#find()方法只接受一个回调函数。

因此,正确的用法是:

message.guild.roles.cache.find(role => role.id === "973945417991589938")

但是,由于 Discord.js 中使用的所有集合都使用它们的id进行映射,并且您想通过它的id找到一个角色,您应该使用get()方法:

message.guild.roles.cache.get("973945417991589938")

暂无
暂无

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

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