node.js - 如何为不和谐机器人创建踢命令?
问题描述
你好,看过这个的人。我需要帮助来为我的不和谐机器人创建踢命令。我正在使用discord.js
和node.js
。我确实有像const Discord = require('discord.js');
const Client = new Discord.Client();
. 我正在使用 Visual Studio Code 对其进行编码。我真的不明白还能做什么。我曾尝试在 YouTube 上寻求帮助,但每次尝试似乎都不再有效。有人可以指导我吗?感谢:D
解决方案
这里...
client.on('message', message => {
// Ignore messages that aren't from a guild
if (!message.guild) return;
// If the message content starts with "!kick"
if (message.content.startsWith('!kick')) {
// Assuming we mention someone in the message, this will return the user
// Read more about mentions over at https://discord.js.org/#/docs/main/master/class/MessageMentions
const user = message.mentions.users.first();
// If we have a user mentioned
if (user) {
// Now we get the member from the user
const member = message.guild.member(user);
// If the member is in the guild
if (member) {
/**
* Kick the member
* Make sure you run this on a member, not a user!
* There are big differences between a user and a member
*/
member
.kick('Optional reason that will display in the audit logs')
.then(() => {
// We let the message author know we were able to kick the person
message.reply(`Successfully kicked ${user.tag}`);
})
.catch(err => {
// An error happened
// This is generally due to the bot not being able to kick the member,
// either due to missing permissions or role hierarchy
message.reply('I was unable to kick the member');
// Log the error
console.error(err);
});
} else {
// The mentioned user isn't in this guild
message.reply("That user isn't in this guild!");
}
// Otherwise, if no user was mentioned
} else {
message.reply("You didn't mention the user to kick!");
}
}
});
(来自discord.js github)
推荐阅读
- sockets - Linux:检查传入的 UDP 数据报
- c# - 如何避免在 ASP.NET MVC 中硬编码资源路径
- javascript - VS代码中的伪元素抛出语法错误
- python - 保护/清理不受信任的客户端对服务器的远程调用
- javascript - PHP JavaScript 客户端 WebSocket 无法接收超过 125 个字节的数据
- jquery - Bootstrap:展开时折叠其他人
- sql - SQLZoo 诺贝尔测验 #3 — 这些语句之间有什么区别?
- javascript - 如何使用 JSDoc 记录返回具有公共方法的对象的工厂函数?
- python - 对 Pandas 系列中的时间序列数据进行反累积
- wordpress - 简码导致白屏死机并破坏了我的 wordpress 网站