Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何发出Discord bot命令?_Javascript_Discord.js - Fatal编程技术网

Javascript 如何发出Discord bot命令?

Javascript 如何发出Discord bot命令?,javascript,discord.js,Javascript,Discord.js,我有一个Discord机器人,我想使用它,这样我就可以禁止用户使用该机器人所在的所有服务器,也就是我拥有的网络 我需要这个命令,因为我有其他项目和东西要处理,这似乎是给我带来最大麻烦的一个。它只需要在主公会中工作,或者我将其切换为角色ID而不是名称 if(command === "sban") { // this is the role of the moderator which would be used in the main guild to ban from all guilds,

我有一个Discord机器人,我想使用它,这样我就可以禁止用户使用该机器人所在的所有服务器,也就是我拥有的网络

我需要这个命令,因为我有其他项目和东西要处理,这似乎是给我带来最大麻烦的一个。它只需要在主公会中工作,或者我将其切换为角色ID而不是名称

if(command === "sban") {
// this is the role of the moderator which would be used in the main guild to ban from all guilds,
    if(!message.member.roles.some(r=>["Moderator"].includes(r.name)) )
      return message.reply("Sorry, you don't have permissions to use this!");

    let member = message.mentions.members.first();
    if(!member)
      return message.reply("Please mention a valid member of this server");
    if(!member.bannable) 
      return message.reply("I cannot ban this user! Do they have a higher role? Do I have ban permissions?");

    let reason = args.slice(1).join(' ');
    if(!reason) reason = "No reason provided";

    await member.ban(reason)
      .catch(error => message.reply(`Sorry ${message.author} I couldn't ban because of : ${error}`));
    message.reply(`${member.user.tag} has been banned by ${message.author.tag} because: ${reason}`);

我知道这个命令本身是有效的,我只是需要一种方法,当我需要禁止某人时,它至少可以在8个以上的行会中工作。

如果我理解的话,你正在尝试从你的机器人所在的所有服务器中禁止一个用户

为此,在
client.guilds
上执行
forEach
循环,并禁止他加入公会

即使该用户不在公会,您仍然可以根据其ID禁止其使用。我举了一个例子:

函数getUserFromTitle(提及){
如果(!提及)返回false;
if(用(“”)表示开始){
提及=提及.切片(2,-1);
如果(用(“!”)提及.startsWith){
提及。切片(1);
返回client.users.get(title);//从title中重新运行用户。
};
};
if(client.users.get(提及)){
return client.users.get(notice);//如果作者提供了ID,则返回用户。
};
};
const UserToBan=getuserfromtoite(参数[0])
const Reason=arguments.slice(12000).join(“”)
如果(!UserToBan)返回message.reply(“请提及用户或提供id”);
如果(!Reason)返回消息。回复(“请提供禁止的原因”);
client.guilds.forEach(guild=>{
if(guild.members.get(UserToBan.id)){
//用户在公会中。
message.reply(${guild.name}中的“禁止用户${UserToBan}”);
帮会禁令(UserToBan,Reason);
}否则{
//该用户不在公会,我们必须通过id禁止他。
message.reply(`禁止${guild.name}!中id为${UserToBan.id}的用户');
guild.ban(UserToBan.id,Reason);
}

});该代码甚至可以缩短为仅按ID禁用,因为它在两种情况下的工作方式完全相同。还要确保捕捉到任何被拒绝的承诺。我将如何准确地修改它,以便按照我需要的方式使用它?