2

我希望我的机器人使用;leave <GuildID>.

下面的代码不起作用:

if (message.guild.id.size < 1)
  return message.reply("You must supply a Guild ID");
if (!message.author.id == 740603220279164939)
  return;

message.guild.leave()
  .then(g => console.log(`I left ${g}`))
  .catch(console.error);
4

1 回答 1

2

您很可能不应该查看message.guild.id,因为它会返回您发送消息的公会 ID。如果您想从中获取公会 ID ;leave (guild id),则必须使用某些东西删除第二部分喜欢.split()

// When split, the result is [";leave", "guild-id"]. You can access the
// guild ID with [1] (the second item in the array).
var targetGuild = message.content.split(" ")[1];

!message.author.id会将作者 ID(在本例中为您的机器人 ID)转换为布尔值,结果为false(因为 ID 已设置且不是值)。我假设您的意思是仅当作者不是机器人本身时才运行此操作,在这种情况下,您很可能针对此:

// You're supposed to use strings for snowflakes. Don't use numbers.
if (message.author.id == "740603220279164939") return;

现在,您只需使用从消息内容中获得的公会 ID 并使用它离开公会即可。为此,只需Guild从您的机器人缓存中获取,然后调用.leave(). 总而言之,您的代码现在应该如下所示:

// Get the guild ID
var targetGuild = message.content.split(" ")[1];
if (!targetGuild) // targetGuild is undefined if an ID was not supplied
    return message.reply("You must supply a Guild ID");

if (message.author.id == "740603220279164939") // Don't listen to self.
    return;

client.guilds.cache.get(targetGuild) // Grab the guild
    .leave() // Leave
    .then(g => console.log(`I left ${g}`)) // Give confirmation after leaving
    .catch(console.error);
于 2020-12-28T18:18:20.940 回答