Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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 我想这样做,分裂将进入(discord.js)_Javascript_Discord.js - Fatal编程技术网

Javascript 我想这样做,分裂将进入(discord.js)

Javascript 我想这样做,分裂将进入(discord.js),javascript,discord.js,Javascript,Discord.js,我正在使用discord.js开发一个discord机器人。它有一个命令,可以用一个命令存储多个东西,比如 !添加Ex1 Ex2 Ex3 分隔符有1个空格。我想让它进入,就像 !add Ex1 Ex2 Ex3 我使用的代码是 const Discord=require(“Discord.js”) module.exports.run=async(客户端、消息、参数)=>{ const channelID=“661653664770818069”; 常量存储=需要(“数据存储”); const

我正在使用discord.js开发一个discord机器人。它有一个命令,可以用一个命令存储多个东西,比如

!添加Ex1 Ex2 Ex3

分隔符有1个空格。我想让它进入,就像

!add Ex1
Ex2
Ex3
我使用的代码是

const Discord=require(“Discord.js”)
module.exports.run=async(客户端、消息、参数)=>{
const channelID=“661653664770818069”;
常量存储=需要(“数据存储”);
const things=newstore({path:“things.json”});
常量随机项=要求(“随机项”);
如果(
message.member.hasPermission('ADMINISTRATOR')){
if(message.content.split(“”.length>=2){
var属性=message.content.split(“”);
attributes.slice(2).forEach(thing=>{
事物。联合(属性[1],事物);
});
message.channel.send(“已成功添加内容”);
}否则{
message.channel.send(“请使用:`!添加(东西)`”);
}
}

};您可以尽可能使用
'\n'
拆分它。 我认为最好的方法是允许使用自我命令
!添加
,然后用列表等待下一条消息。您可以使用此代码执行此操作

const Discord = require("discord.js");
const Store = require("data-store");
const things = new Store({ path: "things.json" });
const randomItem = require("random-item");

module.exports.run = async(client, message, args, item) => {
 if(!message.member.hasPermission('ADMINISTRATOR')) return

      let useTips = new Discord.RichEmbed();
                useTips.setAuthor(message.author.tag, message.author.avatarURL)
                useTips.setDescription('Pls enter the list things separated by new line')
                useTips.setColor(0xdb001d)
      message.channel.send(useTips)
      const filter = m => m.author.id === message.author.id;
      await message.channel.awaitMessages(filter, {max: 1, time: 30000,errors: ['time']})
                .then(collected => {
                        let collection = collected.first();
                        let attributes = collection.content.split("\n");
                        if (attributes.length < 2) {
                            message.channel.send('You need add minimum two things')
                            return
                        }
                        attributes.slice(2).forEach(thing => {
                        things.union(attributes[1], thing);
                        });
                        message.channel.send('things added successfully.')
                })
                .catch(collected => {
          message.channel.send('Timed out');
                    return
                });
}
const Discord=require(“Discord.js”);
常量存储=需要(“数据存储”);
const things=newstore({path:“things.json”});
常量随机项=要求(“随机项”);
module.exports.run=async(客户端、消息、参数、项目)=>{
如果(!message.member.hasPermission('ADMINISTRATOR'))返回
让useTips=newdiscord.RichEmbed();
setAuthor(message.author.tag、message.author.avatarURL)
useTips.setDescription('请输入以新行分隔的列表内容')
useTips.setColor(0xdb001d)
message.channel.send(使用提示)
const filter=m=>m.author.id==message.author.id;
wait message.channel.waitMessages(筛选器,{max:1,时间:30000,错误:['time']})
。然后(收集=>{
let collection=collected.first();
let attributes=collection.content.split(“\n”);
if(attributes.length<2){
message.channel.send('您至少需要添加两件事')
返回
}
attributes.slice(2).forEach(thing=>{
事物。联合(属性[1],事物);
});
message.channel.send('已成功添加内容')
})
.catch(已收集=>{
message.channel.send('Timed out');
返回
});
}

换行符上拆分:
\n
Hmmm,我会检查它。单向键在该块中显示错误。attributes.slice(2.forEach(thing=>{things.union(attributes[1],thing);})`那么,我该怎么做才能修复它,做我喜欢的事情呢!添加列表1,然后它会要求我写列表,我会回复thing1 thing2 thing3和其他内容。json fille将写{“list1”:[“thing1”,“thing2”,“thing3”]}???抱歉,我发现了错误
let collection=collected.first()必须
let collection=collected.first()
About your const things=newstore({path:“things.json”});我不知道你需要什么样的数据结构。但是作为一种选择,您可以简单地使用
push
方法来处理所需的数据。不要忘记保存收到的Json,因为关闭bot后数据将丢失。您可以使用module
fs
和`fs.writeFileSync(“filePatchAndName”,JSON.stringify(YOURJSONArray))`来实现这一点。非常感谢!!!这很有效,你真的很好,比你兄弟❤️❤️❤️