在特定公会的特定频道中编辑消息 | Discord.js
Edit message in a specific channel in a specific guild | Discord.js
你好
(我是法国人,很抱歉我的英语不好)
我想让我的机器人编辑特定频道中的消息,我尝试了很多代码,但 none 其中的代码有效。
let channels = Bot.guilds.find(g => g.id == "guild id").channels.filter(c => c.id == "another guild id").array();
channels.forEach(channel => {
channel.fetchMessage("message id").edit("Message Edited");
);
我也试过 for
等... channel
已定义,但它无法获取任何消息...
我什至不知道我能不能做到...
谢谢你帮助我!
您可以使用 get()
and find()
方法,如下所示。
// Note: This code must be inside of an async function.
const guild = bot.guilds.get('guildIDhere');
if (!guild) return console.log('Unable to find guild.');
const channel = guild.channels.find(c => c.id === 'channelIDhere' && c.type === 'text');
if (!channel) return console.log('Unable to find channel.');
try {
const message = await channel.fetchMessage('messageIDhere');
if (!message) return console.log('Unable to find message.');
await message.edit('Test.');
console.log('Done.');
} catch(err) {
console.error(err);
}
你好
(我是法国人,很抱歉我的英语不好)
我想让我的机器人编辑特定频道中的消息,我尝试了很多代码,但 none 其中的代码有效。
let channels = Bot.guilds.find(g => g.id == "guild id").channels.filter(c => c.id == "another guild id").array();
channels.forEach(channel => {
channel.fetchMessage("message id").edit("Message Edited");
);
我也试过 for
等... channel
已定义,但它无法获取任何消息...
我什至不知道我能不能做到...
谢谢你帮助我!
您可以使用 get()
and find()
方法,如下所示。
// Note: This code must be inside of an async function.
const guild = bot.guilds.get('guildIDhere');
if (!guild) return console.log('Unable to find guild.');
const channel = guild.channels.find(c => c.id === 'channelIDhere' && c.type === 'text');
if (!channel) return console.log('Unable to find channel.');
try {
const message = await channel.fetchMessage('messageIDhere');
if (!message) return console.log('Unable to find message.');
await message.edit('Test.');
console.log('Done.');
} catch(err) {
console.error(err);
}