javascript - 使用图像创建定时静音命令
问题描述
我正在和朋友一起为我的私人不和谐服务器制作一个机器人,我们喜欢星球大战,所以我称之为达斯维德。我一直在看教程,使用这个机器人我已经做得很好,但我被困在一个命令上。此命令称为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 安装,请告诉我。
解决方案
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"});
}
推荐阅读
- java - Arraylist.forEach() 和 Arraylist.replaceAll() 之间的区别
- r - 我应该更新什么来解决 RCurl SSL 版本问题
- pandas - 如何在读取 csv 时停止 pandas 自动转换某些日期
- node.js - 从 gremlin 数据库中获取数据并在 nodejs 中迭代
- ios - Google-firebase 是否允许数据处理逻辑
- javascript - html在textarea旁边对齐表格
- javascript - 尝试执行recaptcha时,gatsby react app中的ref.current为null
- deployment - 两个负载均衡器能否具有相同的 ssl 端点和证书
- c# - 在 C# 中触发 onclick 事件的 HTML 选择元素
- python - 我的图片应该显示在我的鼠标方向,但它不起作用