在这里,我正在尝试制作一个 Discord 机器人,它可以计算服务器中有多少成员,然后将频道名称更改为“成员:”。 这是我到目前为止的代码:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("guildMemberAdd", function(member){
let myGuild = member.guild;
let myChannel = myGuild.channels.cache.get("Channel ID Here");
let memberNumber = myGuild.members.cache.filter(member => !member.user.bot).size;
myChannel.name = `Members: ${memberNumber}`;
})
client.login('000000000000000000000000000000000');
我遇到的问题是,我没有收到任何错误,但是当添加一个新人时,机器人没有做任何事情,绝对没有。我需要什么才能让机器人更改频道的名称。此外,如果有人知道如何使用 Discord 机器人执行此操作,您能否在代码中添加如何使机器人应显示成员数量的文本频道无法访问,就像您可以看到频道名称,但如果您单击它,什么也没有发生,你不能进入频道。
答案 0 :(得分:0)
myChannel.name
只是一个属性,不能直接更改。您需要在频道上调用 .edit()
并传入新名称。
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("guildMemberAdd", function(member){
let myGuild = member.guild;
let myChannel = myGuild.channels.cache.get("Channel ID Here");
let memberNumber = myGuild.members.cache.filter(member => !member.user.bot).size;
myChannel.edit({ name: `Members: ${memberNumber}` });
})
client.login('000000000000000000000000000000000');
答案 1 :(得分:0)
您需要在用户加入和离开服务器时调用一个事件:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("guildMemberAdd", function(member){
memberCount(member);
});
client.on("guildMemberRemove", function(member){
memberCount(member);
});
client.login('000000000000000000000000000000000');
function memberCount(member){
let myGuild = member.guild;
let myChannel = myGuild.channels.cache.get("Channel ID Here");
let memberNumber = myGuild.memberCount;
myChannel.setName({ name: `Members: ${memberNumber.toLocaleString()}` });
}