【问题标题】:Send message to specific channel with typescript使用打字稿向特定频道发送消息
【发布时间】:2019-05-02 23:35:04
【问题描述】:

每当有新用户加入服务器(公会)时,我想向“欢迎”文本频道发送问候消息。

我面临的问题是,当我找到想要的频道时,我会收到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'

我正在使用 11.3.0 版的 discord.js

【问题讨论】:

    标签: node.js typescript discord.js


    【解决方案1】:

    我这样做:

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

    (客户端是不和谐客户端)。如果您将 find 更改为 get 并将频道 ID 放在一些单引号中,您的代码应该可以工作。嗯,它对我有用。

    【讨论】:

    • GuildChannel 类中没有get() 函数。我知道这适用于纯 JavaScript,但我想使用打字稿。因此,我必须遵守所提供的课程。
    • get() 是一个 discord.js 集合函数,它不是 GuildChanel 类的一部分。 guild.channels 返回一个集合,因此它应该可以工作。我认为
    【解决方案2】:

    也许这对你有帮助?

    代码:

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

    【讨论】:

    • GuildChannel 类中没有sendEmbed() 函数。我知道这适用于纯 JavaScript,但我想使用打字稿。因此,我必须遵守所提供的课程。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      也许对于仍在寻找对我有用的答案的后来者

      let channel = client.channels.get("channelid") as Discord.TextChannel;
      channel.send("what you want to send to that channel");
      

      【讨论】:

        【解决方案5】:

        您可以在调用send 之前使用GuildChannel#isText() 方法键入guard。

        例子:

        if (channel.isText()) {
           await channel.send('...');
        }
        

        或者:

        if (!channel.isText()) return;
        await channel.send('...');
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-07-02
          • 2021-08-21
          • 2020-04-04
          • 2018-12-09
          • 2022-01-08
          • 2021-01-26
          相关资源
          最近更新 更多