首页 > 解决方案 > 使用图像创建定时静音命令

问题描述

我正在和朋友一起为我的私人不和谐服务器制作一个机器人,我们喜欢星球大战,所以我称之为达斯维德。我一直在看教程,使用这个机器人我已经做得很好,但我被困在一个命令上。此命令称为forcechoke. 它的作用是在聊天中添加一条消息:

Darth Vader:Forcechokes @fakeplayer (时间以秒为单位)。 我在文件夹中包含我所有代码的附件(Darth Vader 窒息某人)。

基本上,它会将该人静音 60 秒,然后显示达斯维达正在窒息某人。命令:!forcechoke <@person> <time in seconds>. 我已经!forcechoke完成了,你只需要看看。

const commando = require('discord.js-commando');

class ForceChokeCommand extends commando.Command
{
    constructor(client)
    {
        super(client,{
            name: 'forcechoke',
            group: 'sith',
            memberName: 'forcechoke',
            description: 'Darth Vader will choke the person of your choice!',
            args: [
                {
                    key: 'user',
                    prompt: 'Who would you like me to forcechoke?',
                    type: 'user'
                }
            ]
        });
    }

// THIS IS WHERE I NEED HELP

  }
}

module.exports = ForceChokeCommand;

另外,如果我需要 npm 安装,请告诉我。

标签: javascriptdiscord.jscommando

解决方案


1. 使用户静音:
没有内置的方法可以使用户静音,您必须使用角色来做到这一点。创建一个角色(假设它称为Mute)并撤销每个频道中的“Send Messages”、“Connect”、“Speak”等权限。然后为该角色分配如下内容:

run(msg) {
  let mute_role = msg.guild.roles.find("name", "Mute");
  let member = msg.mentions.members.first();
  member.addRole(mute_role); // <- this assign the role
  setTimeout(() => {member.removeRole(mute_role);}, 60 * 1000); // <- sets a timeout to unmute the user.
}

guild.channels.forEach()或者,您可以使用和覆盖每个频道中该用户(甚至角色)的权限GuildChannel.overwritePermissions(),这取决于您。

2. 发送图片:
您可以​​使用在线图片的 URL 或路径:

msg.say(`Forcechockes ${member} for 60 seconds.`, {file: "path or URL as a string"});

- 回顾:
创建一个名为“静音”的角色(或任何您想要的,只需在代码中替换“静音”)。
找到一张图片,然后您既可以从网络上使用它,也可以将其保存在本地。我将从网上获取一个,您可以将我的 URL 替换为另一个 URL 或文件的本地路径。
将此代码添加到您的命令文件中:

run(msg) {
  let mute_role = msg.guild.roles.find("name", "Mute"); // this is where you can replace the role name
  let member = msg.mentions.members.first();
  member.addRole(mute_role); // <- this assign the role
  setTimeout(() => {member.removeRole(mute_role);}, 60 * 1000); // <- sets a timeout to unmute the user.
  //                                                       V this is where the URL or the local path goes                                    V
  msg.say(`Forcechockes ${member} for 60 seconds.`, {file: "https://lumiere-a.akamaihd.net/v1/images/databank_forcechoke_01_169_93e4b0cf.jpeg"});
}

推荐阅读