如何在基于 webhook 和角色的 JS 中修复复杂的 Discord 命令
Posted
技术标签:
【中文标题】如何在基于 webhook 和角色的 JS 中修复复杂的 Discord 命令【英文标题】:How to fix a complex Discord command in JS based off of webhooks and roles 【发布时间】:2019-09-10 05:06:19 【问题描述】:我正在处理一个命令,当你执行命令时,d!woa 会发生以下情况
使用特定名称创建一个 webhook,然后使用通道名称创建一个角色,之后机器人会监视该通道是否存在具有该特定名称的 webhook,并查看是否有人在该通道中发送消息.如果是这样,那么机器人将添加具有特定名称的角色。 问题是有这个错误:
TypeError: Cannot read property 'guild' of undefined
该错误很可能出现在所提供代码的末尾。
我尝试重新排列代码、定义公会和定义消息。即使尝试了所有这些,它似乎也不起作用。我只希望它依赖 ID 而不是 Name 来准确执行此命令。
const Discord = require('discord.js');
const commando = require('discord.js-commando');
class woa extends commando.Command
constructor(client)
super(client,
name: 'watchoveradd',
group: 'help',
memberName: 'watchoveradd',
description: 'placeholder',
aliases: ['woa'],
)
async run(message, args)
if (message.channel instanceof Discord.DMChannel) return message.channel.send('This command cannot be executed here.')
else
if(!message.member.guild.me.hasPermission(['MANAGE_WEBHOOKS'])) return message.channel.send('I don\'t have the permissions to make webhooks, please contact an admin or change my permissions!')
if(!message.member.guild.me.hasPermission(['MANAGE_ROLES'])) return message.channel.send('I don\'t have the permissions to make roles, please contact an admin or change my permissions!')
if (!message.member.hasPermission(['MANAGE_WEBHOOKS'])) return message.channel.send('You need to be an admin or webhook manager to use this command.')
if (!message.member.hasPermission(['MANAGE_ROLES'])) return message.channel.send('You need to be an admin or role manager to use this command.')
const avatar = `https://cdn.discordapp.com/attachments/515307677656678420/557050444954992673/Generic5.png`;
const name2 = "SYNTHIBUTWORSE-1.0WOCMD";
let woaID = message.mentions.channels.first();
if(!woaID) return message.channel.send("Channel is nonexistant or command was not formatted properly. Please do s!woa #(channelname)");
let specifiedchannel = message.guild.channels.find(t => t.id == woaID.id);;
specifiedchannel.send('test');
const hook = await woaID.createWebhook(name2, avatar).catch(error => console.log(error))
await hook.edit(name2, avatar).catch(error => console.log(error))
message.channel.send("Please do not tamper with the webhook or else the command implied before will no longer function with this channel.")
setTimeout(function()
message.channel.send('Please wait...');
, 10);
setTimeout(function()
var role = message.guild.createRole(
name: `Synthibutworse marker $woaID.name v1.0`,
color: 0xcc3b3b,).catch(console.error);
if(role.name == "Synthibutworse marker")
role.setMentionable(false, 'SBW Ping Set.')
role.setPosition(10)
role.setPermissions(['CREATE_INSTANT_INVITE', 'SEND_MESSAGES'])
.then(role => console.log(`Edited role`))
.catch(console.error);
, 20);
var sbwrID = member.guild.roles.find(`Synthibutworse marker $woaID v1.0`);
let specifiedrole = message.guild.roles.find(r => r.id == sbwrID.id)
setTimeout(function()
message.channel.send('Created Role... Please wait.');
, 100);
message.guild.specifiedchannel.replacePermissionOverwrites(
overwrites: [
id: specifiedrole,
denied: ['SEND_MESSAGES'],
allowed: ['VIEW_CHANNEL'],
,
],
reason: 'Needed to change permissions'
);
var member = client.user
var bot = message.client
bot.on('message', function(message)
if(message.channel.id == sbwrID.id)
let bannedRole = message.guild.roles.find(role => role.id === specifiedrole);
message.member.addRole(bannedRole);
)
;
module.exports = woa;
我期望一个没有 TypeError 的命令,并且该命令能够创建一个角色和一个 webhook(用于标记),并且该角色是自动设置的,因此拥有该角色的用户将无法发言频道,在频道中发言的人将获得该角色。
实际输出是 TypeError: Cannot read property 'guild' of undefined
,但创建了一个角色和 webhook。
【问题讨论】:
【参考方案1】:你有var sbwrID = member.guild...
您没有定义成员。使用message.member.guild...
您可以设置一个 linter (https://discordjs.guide/preparations/setting-up-a-linter.html) 来自动发现这些问题。
【讨论】:
这行得通,但在此之后出现了另一个问题。现在是Error: Value must be specified.
如果您可以获得堆栈跟踪信息会有所帮助以上是关于如何在基于 webhook 和角色的 JS 中修复复杂的 Discord 命令的主要内容,如果未能解决你的问题,请参考以下文章
如何在设置 jenkins 触发器时在 github webhook 中修复“我们无法传递此有效负载:无法连接到服务器”?
使用 Discord.js,如何在触发 webhook 时触发机器人?