【问题标题】:Change whole server's usernames with discord.js v12使用 discord.js v12 更改整个服务器的用户名
【发布时间】:2021-07-09 20:25:46
【问题描述】:

我在更改不和谐服务器中每个人的用户名时遇到问题。我正在使用 discord.js v12,服务器中有大约 20 人。我是它的所有者,我的机器人有管理员,但机器人只更改语音频道中人员的用户名。这是我的代码:

module.exports = {
    name: 'username',
    description: "Changes the whole server's usernames",
    execute(message, args, prefix, Discord, client) {
        let nick = message.content.slice((prefix + "username").length);
        if (!args[0]) {
            message.channel.send("You didn't specify what to change them to!");
        }
        else {
            message.guild.members.cache.forEach(r => {
                if (r.id == '855161030119129090') // This is to not change the username of the bot
                    return;
                r.setNickname(nick);
            });
            message.channel.send('Usernames changed.');
        }
    }
}

【问题讨论】:

  • 大量 API 调用属于 API 滥用。您的客户端可能会被 Discord API 暂停
  • 以下是 Discord API 限制:discord.com/developers/docs/topics/…。 tl;dr All bots can make up to 50 requests per second to our API. 也许你的机器人已经尝试超过 50 个请求?

标签: javascript discord.js


【解决方案1】:

缓存并不包含服务器中的所有成员,因此您必须获取它们才能获取服务器的所有成员。您可以使用fetch 方法来做到这一点。

这是一个例子:

client.on("message", async (message) => {
    if (message.content.toLowerCase() === prefix + "username") {
    await message.guild.members.fetch(); // If you don't provide a parameter, it will fetch all the members
    
    // Rest of the code...
  }
});

【讨论】:

    猜你喜欢
    • 2020-10-04
    • 2018-04-04
    • 2021-05-17
    • 1970-01-01
    • 2021-09-14
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多