使用打字稿将消息发送到特定频道
Send message to specific channel with typescript
我想在新用户加入服务器(公会)时向 "welcome" 文本频道发送问候消息。
我面临的问题是,当我找到想要的频道时,我将收到类型为 GuildChannel
的频道。
由于GuildChannel
没有send()
功能,我无法发送消息。但是我找不到找到 TextChannel
的方法,所以我被困在这里。
如何访问 TextChannel
以便我能够使用 send()
消息?在我现在使用的代码下方:
// Get the log channel (change to your liking)
const logChannel = guild.channels.find(123456);
if (!logChannel) return;
// A real basic message with the information we need.
logChannel.send('Hello there!'); // Property 'send' does not exist on type 'GuildChannel'
我使用的是 discord.js
的 11.3.0 版
我这样做:
let channel = client.guilds.get('your-guild-id').channels.get('your-channel-id');
channel.send("it worked");
(客户端是discord客户端)。如果您将 find
更改为 get
并将频道 ID 放在一些单引号中,您的代码应该可以工作。嗯,对我有用。
也许这对你有帮助?
代码:
client.on('guildMemberAdd', member => {
let channel = member.guild.channels.find('name', 'welcome');
let memberavatar = member.user.avatarURL
if (!channel) return;
let embed = new Discord.RichEmbed()
.setColor('RANDOM')
.setThumbnail(memberavatar)
.addField(':bust_in_silhouette: | name : ', `${member}`)
.addField(':microphone2: | Welcome!', `Welcome to the server, ${member}`)
.addField(':id: | User :', "**[" + `${member.id}` + "]**")
.addField(':family_mwgb: | Your are the member', `${member.guild.memberCount}`)
.addField("Name", `<@` + `${member.id}` + `>`, true)
.addField('Server', `${member.guild.name}`, true )
.setFooter(`**${member.guild.name}**`)
.setTimestamp()
channel.sendEmbed(embed);
});
多亏了这个 GitHub issue 我找到了解决问题的方法。
我需要使用 Type Guard 来缩小正确的类型。
我现在的代码是这样的:
// Get the log channel
const logChannel = member.guild.channels.find(channel => channel.id == 123456);
if (!logChannel) return;
// Using a type guard to narrow down the correct type
if (!((logChannel): logChannel is TextChannel => logChannel.type === 'text')(logChannel)) return;
logChannel.send(`Hello there! ${member} joined the server.`);
也许对于仍在寻找答案的后来者来说这对我有用
let channel = client.channels.get("channelid") as Discord.TextChannel;
channel.send("what you want to send to that channel");
您可以在调用send
之前使用GuildChannel#isText()
方法键入guard。
示例:
if (channel.isText()) {
await channel.send('...');
}
或者:
if (!channel.isText()) return;
await channel.send('...');
我想在新用户加入服务器(公会)时向 "welcome" 文本频道发送问候消息。
我面临的问题是,当我找到想要的频道时,我将收到类型为 GuildChannel
的频道。
由于GuildChannel
没有send()
功能,我无法发送消息。但是我找不到找到 TextChannel
的方法,所以我被困在这里。
如何访问 TextChannel
以便我能够使用 send()
消息?在我现在使用的代码下方:
// Get the log channel (change to your liking)
const logChannel = guild.channels.find(123456);
if (!logChannel) return;
// A real basic message with the information we need.
logChannel.send('Hello there!'); // Property 'send' does not exist on type 'GuildChannel'
我使用的是 discord.js
的 11.3.0 版我这样做:
let channel = client.guilds.get('your-guild-id').channels.get('your-channel-id');
channel.send("it worked");
(客户端是discord客户端)。如果您将 find
更改为 get
并将频道 ID 放在一些单引号中,您的代码应该可以工作。嗯,对我有用。
也许这对你有帮助?
代码:
client.on('guildMemberAdd', member => {
let channel = member.guild.channels.find('name', 'welcome');
let memberavatar = member.user.avatarURL
if (!channel) return;
let embed = new Discord.RichEmbed()
.setColor('RANDOM')
.setThumbnail(memberavatar)
.addField(':bust_in_silhouette: | name : ', `${member}`)
.addField(':microphone2: | Welcome!', `Welcome to the server, ${member}`)
.addField(':id: | User :', "**[" + `${member.id}` + "]**")
.addField(':family_mwgb: | Your are the member', `${member.guild.memberCount}`)
.addField("Name", `<@` + `${member.id}` + `>`, true)
.addField('Server', `${member.guild.name}`, true )
.setFooter(`**${member.guild.name}**`)
.setTimestamp()
channel.sendEmbed(embed);
});
多亏了这个 GitHub issue 我找到了解决问题的方法。
我需要使用 Type Guard 来缩小正确的类型。
我现在的代码是这样的:
// Get the log channel
const logChannel = member.guild.channels.find(channel => channel.id == 123456);
if (!logChannel) return;
// Using a type guard to narrow down the correct type
if (!((logChannel): logChannel is TextChannel => logChannel.type === 'text')(logChannel)) return;
logChannel.send(`Hello there! ${member} joined the server.`);
也许对于仍在寻找答案的后来者来说这对我有用
let channel = client.channels.get("channelid") as Discord.TextChannel;
channel.send("what you want to send to that channel");
您可以在调用send
之前使用GuildChannel#isText()
方法键入guard。
示例:
if (channel.isText()) {
await channel.send('...');
}
或者:
if (!channel.isText()) return;
await channel.send('...');