【问题标题】:Discord bot join/leave channel change separately per guildDiscord bot 加入/离开频道分别更改每个公会
【发布时间】:2021-06-24 04:30:22
【问题描述】:

我正在尝试找出如何制作一个命令来检测来自不同行会的频道(等等。$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}`);
    }
 }
 });

【问题讨论】:

  • 您可以使用数组或对象来存储与频道 ID 相关的公会,但这样的缓存会在启动机器人时重置。最好的情况是使用数据库并查询它以找到频道。

标签: discord.js


【解决方案1】:

我猜你可以使用 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

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

【讨论】:

  • 好的,但是如何从每个服务器的 json 文件中获取 id
  • 上面写着chans['G'+guild.id]
  • 对于chans guild的事情,它没有在json文件中添加另一个id
  • 如果我要添加一个数据库,那将如何工作?
  • 我编辑了一些东西,确保不是 guild.id,改成 msg.guild.id
猜你喜欢
  • 2020-07-14
  • 1970-01-01
  • 2019-05-05
  • 2018-12-03
  • 2020-11-04
  • 2020-11-03
  • 1970-01-01
  • 2022-01-23
  • 2021-01-05
相关资源
最近更新 更多