首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用图像创建时间静音命令

使用图像创建时间静音命令
EN

Stack Overflow用户
提问于 2018-07-27 07:24:50
回答 2查看 6.4K关注 0票数 0

我正在为我与朋友的私人不和服务器制作一个机器人,我们喜欢“星球大战”,所以我把它叫做达斯·维德。我一直在看教程,有了这个机器人,我变得很棒,但是我被命令困住了。这个命令称为forcechoke。它所做的就是在聊天中传递信息:

达斯·维德: Forcechokes @fakeplayer (时间以秒为单位)。附件文件(达斯维德窒息某人),我有在文件夹中与我的所有代码。

基本上,它使人沉默60秒,然后显示达斯维德窒息某人。命令:!forcechoke <@person> <time in seconds>。我已经做好了!forcechoke,你只需要看一看。

代码语言:javascript
复制
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安装,请告诉我。

EN

回答 2

Stack Overflow用户

发布于 2018-07-27 12:06:38

1.静音用户:

没有内置的方式来静音一个用户,你将不得不用角色来做。创建一个角色(假设它被称为Mute),并在每个通道中撤销诸如“发送消息”、“连接”、“说话”等权限。然后用这样的东西来分配这个角色:

代码语言:javascript
复制
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或路径:

代码语言:javascript
复制
msg.say(`Forcechockes ${member} for 60 seconds.`, {file: "path or URL as a string"});

- Recap:

创建一个名为"Mute“的角色(或者任何您想要的角色,只需在代码中替换"Mute”)。

找到一个图像,然后你可以使用它从网络或保存在本地。我会从网上取一个,你可以用另一个URL或者你的文件的本地路径来代替我的URL。

将此代码添加到命令文件中:

代码语言:javascript
复制
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"});
}
票数 0
EN

Stack Overflow用户

发布于 2018-07-28 07:52:43

用定时器静音

我的回答基本上是费德里科·格兰迪( Federico )所做的,但更多的是冲了出来,并以不和谐的方式实施。在扩展的run()类中添加此Command方法。run()是命令中用于执行用户请求启动的进程的主要方法。

给你:

代码语言:javascript
复制
async run(message, args) {
     // get the user from the required args object
    const userToMute = message.guild.members.find('id', args.user.id);
    
    // find the name of a role called Muted in the guild that the message
    // was sent from
    const muteRole = message.guild.roles.find("name", "Muted");

    // add that role to the user that should be muted
    userToMute.addRole(muteRole);

    // the time it takes for the mute to be removed
    // in miliseconds
    const MUTE_TIME = 60 * 1000;

    // wait MUTE_TIME miliseconds and then remove the role
    setTimeout(() => {
        userToMute.removeRole(muteRole);
    }, MUTE_TIME);

    message.channel.send(`*${message.author.username} forcechockes ${userToMute.user.username} for ${MUTE_TIME / 60} seconds*`, { file: 'url/path' });
    return;
}

如果您想知道javascript中的async关键字,这是一个相当新的东西,但它只允许您运行此方法,而无需让bot等待它完成。

setTimeout()是一个全局 javascript函数,它简单地告诉您的程序在运行该进程之前等待一定时间。

() => {}在其基础上是function helloworld() {}的缩写

`${}`奇怪的字符串格式尝试读取一些

希望这有帮助,有乐趣地学习一些javascript :)!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51552849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档