Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 验证特定通道中的命令和不带前缀的特定消息_Javascript_Discord.js - Fatal编程技术网

Javascript 验证特定通道中的命令和不带前缀的特定消息

Javascript 验证特定通道中的命令和不带前缀的特定消息,javascript,discord.js,Javascript,Discord.js,我基本上是在尝试编写一些代码,其中您需要将消息发送到特定的频道,并发送特定的消息以获得特定的角色,基本上就像这样验证,但没有前缀,但错误不断出现,我无法摆脱它 我正在尝试修复的脚本: bot.on("message", async message => { if(message.channel.id === '753928667427635230'){ if(message.content == 'test'){ let role = m

我基本上是在尝试编写一些代码,其中您需要将消息发送到特定的频道,并发送特定的消息以获得特定的角色,基本上就像这样验证,但没有前缀,但错误不断出现,我无法摆脱它

我正在尝试修复的脚本:

bot.on("message", async message => {
if(message.channel.id === '753928667427635230'){
        if(message.content == 'test'){
        let role = message.guild.roles.cache.find('757964609222344744')
        let member = message.author.id
        member.roles.add(role)
        message.channel.send('script works')
    }
}
错误代码如下:

(node:14284) UnhandledPromiseRejectionWarning: TypeError: fn is not a function
at Map.find (C:\Users\haxor\Desktop\HardWareProject\node_modules\@discordjs\collection\dist\index.js:161:17)
at Client.<anonymous> (C:\Users\haxor\Desktop\HardWareProject\main.js:37:50)
at Client.emit (events.js:315:20)
at MessageCreateAction.handle (C:\Users\haxor\Desktop\HardWareProject\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (C:\Users\haxor\Desktop\HardWareProject\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
(节点:14284)未处理的PromisejectionWarning:TypeError:fn不是函数
在Map.find(C:\Users\haxor\Desktop\HardWareProject\node\u modules\@discordjs\collection\dist\index.js:161:17)
在客户端。(C:\Users\haxor\Desktop\HardWareProject\main.js:37:50)
在Client.emit(events.js:315:20)
在MessageCreateAction.handle(C:\Users\haxor\Desktop\HardWareProject\node\u modules\discord.js\src\client\actions\MessageCreate.js:31:14)
在Object.module.exports[作为消息\u CREATE](C:\Users\haxor\Desktop\HardWareProject\node\u modules\discord.js\src\client\websocket\handlers\MESSAGE\u CREATE.js:4:32)
我的整个剧本:

      const Discord = require('discord.js')
      const bot = new Discord.Client()
      const botsettings = require('./botsettings.json')
      const fs = require('fs')

            bot.commands = new Discord.Collection();
            bot.aliases = new Discord.Collection();

            fs.readdir("./commands/", (err, files) => {
            if(err) console.log(err);

            let jsfile = files.filter(f => f.split('.').pop() === 'js');
            if(jsfile.length <= 0){
            console.log('Commands aren\'t found!');
            return;
    }

            jsfile.forEach((f) => {
            let props = require(`./commands/${f}`);
            console.log(`${f} loaded!`)
            bot.commands.set(props.help.name, props);

            props.help.aliases.forEach(alias => {
            bot.aliases.set(alias, props.help.name);
        })
    })
})

            bot.on("ready", async () => {
            console.log(`Logged in as ${bot.user.username}`);
            bot.user.setActivity(`HardLies`, {type: 'WATCHING' });
})
            // Where the current error is at
            bot.on("message", async message => {
            if(message.channel.id === '753928667427635230'){
            if(message.content == 'test'){
            let role = message.guild.roles.cache.find('757964609222344744')
            let member = message.author.id
            member.roles.add(role)
            message.channel.send('script works')
    }
}

            if(message.channel.type === 'dm') return;
            if(message.author.bot) return;

            let prefix = botsettings.prefix;

            if(!message.content.startsWith(prefix)) return;
            let args = message.content.slice(prefix.length).trim().split(/ +/g);
            let cmd;
            cmd = args.shift().toLowerCase();
            let command;
            let commandFile = bot.commands.get(cmd.slice(prefix.length));
            if(commandFile) commandFile.run(bot, message, args);

            if(bot.commands.has(cmd)){
            command = bot.commands.get(cmd);
    } else if(bot.aliases.has(cmd)){
            command = bot.commands.get(bot.aliases.get(cmd));
}
try {
            command.run(bot, message, args);
} catch (e){
            return;
}

});

            bot.login(botsettings.token);
const Discord=require('Discord.js')
const bot=new Discord.Client()
const botsettings=require('./botsettings.json')
常量fs=require('fs')
bot.commands=new Discord.Collection();
bot.alias=new Discord.Collection();
fs.readdir(“./commands/”,(错误,文件)=>{
if(err)console.log(err);
让jsfile=files.filter(f=>f.split('.').pop();
如果(jsfile.length错误“fn不是函数”是在您使用
时出现的。查找(值)
,但这是一个不正确的用法。请尝试
.find(u=>u.id====id)
(对于成员
.find(m=>m.user.id==id)
或使用
获取(id)

其次,您正试图向
用户
对象添加角色。这是不可能的。您必须使用
GuildMember
对象,因此请改用:

const user=message.member
最后,要添加角色,您必须在添加函数之前使用
cache

member.roles.cache.add(角色)
您的代码应该如下所示:

bot.on(“消息”,异步消息=>{
如果(message.channel.id=='753928667427635230'){
如果(message.content=='test'){
让role=message.guild.roles.cache.get('757964609222344744')
让成员=message.author.id
member.roles.cache.add(角色)
message.channel.send('脚本工作')
}
}
错误“fn不是函数”是在您使用
时出现的。查找(值)
但这是一个不正确的用法。请尝试
.find(u=>u.id===id)
(对于成员
.find(m=>m.user.id==id)
或使用
.get(id)

其次,您正试图向
用户
对象添加角色。这是不可能的。您必须使用
GuildMember
对象,因此请改用:

const user=message.member
最后,要添加角色,您必须在添加函数之前使用
cache

member.roles.cache.add(角色)
您的代码应该如下所示:

bot.on(“消息”,异步消息=>{
如果(message.channel.id=='753928667427635230'){
如果(message.content=='test'){
让role=message.guild.roles.cache.get('757964609222344744')
让成员=message.author.id
member.roles.cache.add(角色)
message.channel.send('脚本工作')
}
}

看起来您试图用括号而不是最后一行的大括号来关闭and if语句。哦,这只是我做错了一件事,而您的代码中根本没有使用这个fn,这让我怀疑错误不是来自您提供的代码。您能提供完整的代码吗?使用
.get()
而不是
.find()
for ids看起来您试图用括号而不是最后一行的大括号来关闭and if语句。哦,这只是我做错事,而fn在您的代码中根本没有使用,这让我怀疑错误不是来自您提供的代码。您能提供完整的代码吗?使用
.get()
而不是ID的
.find()