繁体   English   中英

Discord.py 无法使用单独的功能在特定频道中发送消息

[英]Discord.py cannot send message in specific channel using separate function

我尝试制作一个函数,该函数将接受textchannel_id参数并将嵌入发送到特定频道。 出于某种原因,bot 没有向频道发送任何内容。 我做错了什么?

async def QuickEmbed(text, channelId):
    channel = bot.get_channel(channelId)
    em = discord.Embed(title="\u200b", description=text, color="#f1c40f")
    await channel.send(embed=em)

@bot.command()
async def log(ctx):
    await QuickEmbed("test message", 123456789)```

这是一个很好的方法:

async def log(
    self,
    ctx,
    desc,
    title='**Title**',
    color=0xff0000,
    **kwargs
):

    guild = ctx.guild
    log_embed = discord.Embed(
        title=title,
        description=desc,
        color=color
    )
    for key, value in kwargs.items():
        if key == 'fields':
            for field in value:
                if len(field) == 2:
                    log_embed.add_field(
                        name=field[0],
                        value=field[1]
                    )
                else:
                    log_embed.add_field(
                        name=field[0],
                        value=field[1],
                        inline=field[2]
                    )
        if key == 'showauth':
            if value:
                author = ctx.author
                disp_name = author.display_name
                icon_url = author.avatar_url
                log_embed.set_author(
                    name=disp_name,
                    icon_url=icon_url
                )
                log_embed.set_thumbnail(
                    url=icon_url
                )
    now = datetime.now()
    log_embed.timestamp = now
    log_channel = discord.utils.get(guild.text_channels, name="channel_name")
    await log_channel.send(embed=log_embed)

还有一个使用它的快速示例:

        await self.log(
            ctx,
            'tdescription here',
            'title here',
            discord.Color.blue(),
            fields=[
                ('**Field**', "field value, you can add more fields")
            ],
            showauth=True
        )
        # you can use message, title, color, field(s), and the showauth

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM