每个公会的 Discord bot 加入/离开频道单独更改

时间:2021-06-23 00:22:33

标签: discord.js

我试图找出如何制作一个命令来检测来自不同公会的频道(等等。$setwelcome #channel)。我已经发出命令,但不是为一个公会设置它,而是为所有公会设置它。这是我的代码

client.on('guildMemberAdd', member => {
console.log("New member joined.");

console.log(`Matching on joinChannel: ${joinChannel}`);
const channelID = joinChannel.toString().match(/\d+/)[0];
const channel = member.guild.channels.cache.get(channelID);
console.log(`Fetched channel with ${channelID}`);



// Do nothing if the channel wasn't found on this server
if (!channel){
  console.log("The joinChannel does not exist.");
}else{
  // Send the message, mentioning the member
  channel.send(`Welcome to the server, ${member}`);
  member.roles.add(member.guild.roles.cache.find(i => i.name === 'member'));
}
});
/*const channel = member.guild.channels.cach.find((ch) => {
console.log(ch.name);
return ch.name === joinChannel;*/

client.on('guildMemberRemove', member =>{

console.log(`Matching on joinChannel: ${joinChannel}`);
const channelID = joinChannel.toString().match(/\d+/)[0];
const channel = member.guild.channels.cache.get(channelID);
console.log(`Fetched channel with ${channelID}`);
// Do nothing if the channel wasn't found on this server
if (!channel) return;
// Send the message, mentioning the member
channel.send(`Goodbye ${member}, we will miss you :cry:`);
})

client.on("message", message => {
if (!message.author.bot){
    const content = message.content;
    if (content.toLowerCase().startsWith(`${prefix}setwelcome`)){
        joinChannel = content.substring((`${prefix}setwelcome`).length).trim();
        console.log(`Join channel changed to ${joinChannel}`);
    }
 }
 });

1 个答案:

答案 0 :(得分:0)

我猜你可以使用 JSON,最好是在数据库中,或者其他文件中:

//how the JSON should look like
{
"G123456789012345678": "123456789012345678"
}
//first part is the guild ID, second, is the id the channel you choose

现在你必须以某种方式修改这些数据,为此我将使用 fs,它假定这是在文件系统中。我会像在同一个文件夹中一样引用它,并命名为:welcomeChannels.json

const fs = require('fs');
//maybe other "requires"
client.on('message', msg => {
//checking message content etc
let ChansString = fs.readFileSync('./welcomeChannels.json');
let chans = JSON.parse(ChansString);
//you can get the channel for the guild with chans['G'+guild.id]
chans['G'+msg.guild.id] = msg.mentions.channels.first().id || msg.channel.id;
fs.writeFileSync('./welcomeChannels.json', JSON.stringify(chans));
})
//use chans[`G${guild.id}`] to get the welcome channel id

警告:这可能会填满您的存储空间。您应该改用数据库。