我正在尝试让我的不和谐机器人能够模仿人,就像爸爸Bot一样,但是要使用自定义文字。我当前的代码是:
else if (command === 'sudo') {
const sudoname = message.mentions.users.first().username
if (sudoname === null) {
return message.channel.send ('you need to mention someone.')
}
//remove the command
const sudoreplace = message.content.replace('/sudo ', "")
//remove the mention, we are left with the message
const sudoreplace2 = sudoreplace.replace(args[0], "")
const webhookpfp = message.mentions.users.first()
//get mentioned user's avatar
const webhookpfp2 = webhookpfp.avatarURL()
const sudochannel = message.channel.id
fetch(
'https://discord.com/api/webhooks/xxxxxxxxxxxxxxxxxxxxxx/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
{
method: 'post',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: sudoname,
avatar_url:
webhookpfp2,
content:
sudoreplace2,
})
}
)
}
我知道替换数据的方式很奇怪,但是它可以正常工作,它从Webhook发送带有所提及用户的配置文件和名称的消息,但是它仅在服务器Webhook设置中设置的一个通道中执行此操作。有没有一种方法可以将Webhook通道编辑为执行命令的通道?
这是它的图像:https : //i.stack.imgur.com/pjJrD.png
像这样使用discord.js的WebhookClient
(Implements Webhook
)类:
const { Webhook } = require('discord.js');
const webhook = new Webhook('xxxxxxxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
然后WebHook#send
,您可以使用WebhookMessageOptions
:
webhook.send(sudoreplace2, {avatarURL: webhookpfp2, username: sudoname});
在此之前,您可以使用更改频道WebHook#edit
:
webhook.edit({channel: message.channel});
最终总结:
const { Webhook } = require('discord.js');
const webhook = new Webhook('xxxxxxxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
webhook.edit({channel: message.channel});
webhook.send(sudoreplace2, {avatarURL: webhookpfp2, username: sudoname});
我遇到的问题:我(公会所有者)创建了Webhook,当Bot尝试对其进行编辑时,Discord API返回了DiscordAPIError: 401: Unauthorized
,我通过使用来解决了这个问题message.channel.createWebhook('PlaceHolder');
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句