Send message to specific channel with typescript

試著忘記壹切 提交于 2019-12-06 11:19:00

Thanks to this GitHub issue I've found the solution to my problem.

I need to use a Type Guard to narrow down the correct type.

My code now is this:

// 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.`);

I do this:

let channel = client.guilds.get('your-guild-id').channels.get('your-channel-id');
channel.send("it worked");

(client is the discord client). your code should work if you change find to get and put the channel id in some single quotes. Well, it works for me.

Maybe this can help you?

Code:

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);
});

Maybe for latecomers who are still looking for an answer this worked for me

let channel = client.channels.get("channelid") as Discord.TextChannel;
channel.send("what you want to send to that channel");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!