javascript - discord.js v13 交互按钮删除原始消息
问题描述
const row = new Discord.MessageActionRow()
.addComponents(
new Discord.MessageButton()
.setCustomId(`deletable`)
.setLabel('❌')
.setStyle(4)
);
user.send({content: 'hi', components: [row]});
单击按钮时:
client.ws.on('INTERACTION_CREATE', async (interaction) => {
const {
data: {
custom_id
}
} = interation;
if (custom_id && custom_id === "deletable") {
let channel = await client.messages.fetch({
around: interaction.message.id,
limit: 1
}).then((msg) => {
const fetchedMsg = msg.first();
console.log(msg);
fetchedMsg.delete();
});
}
});
如何删除单击按钮的消息?(DM)
我找不到从 dm 发送的消息的频道。
日志:
TypeError: Cannot read properties of undefined (reading 'fetch')
解决方案
根据文档,有一个interaction.channel
属性可供您使用:
if (custom_id && custom_id === "deletable") {
const channel = interaction.channel;
const fetchedMsg = await channel.messages.fetch({ around: interaction.message.id, limit: 1 });
await fetchedMsg.delete();
// Alternatively, you could also use this:
await interaction.message.delete(); // :)
}
推荐阅读
- java - 更新 SQLite 数据库 Android RecyclerView 迭代
- javascript - Javascript递归回溯数独求解器
- php - 当 $stmt->execute() 使我的 apache 服务器崩溃时,如何输出 MySQLi $stmt?
- r - 决定 scale_y_continuous 比例因子的密度函数 ggplot2
- laravel - 密码接收。Laravel 控制器依赖注入
- java - java中的get(int index)方法
- android - 如何在 Android 应用程序中显示来自网站的 iframe?
- protractor - 量角器测试未完成,似乎循环直到茉莉超时错误命中
- android-x86 - 如何增加 Bliss OS 双启动安装的内部存储
- elf - 在 ELF 文件中获取虚拟地址和偏移量之间的差异