Discord.py从频道名称中拆分表情符号

2024-09-29 23:17:53 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试在类别中创建频道列表,然后拆分频道名称和表情。然后从中发送一个嵌入

问题在于,有时域名类似于#域名表情符号:

我使用-来分割姓名,但在这种情况下我不能这样做,所以有没有办法检查表情符号是否是实际的表情符号?然后使用相同的表情符号,将反应添加到机器人发送的嵌入中

我在这里用评论标记了这些问题。有没有更好的方法来分割表情符号和域名

到目前为止,我有:

@commands.command()
    async def checkin(self, ctx):
      if ctx.guild.id != "guild id":
        return
      else:
        category = discord.utils.get(ctx.guild.categories, id="my ID here")
        checkinem = discord.Embed(title='Realms Channels')
        
        for channel in category.channels:
          realm, emoji = channel.name.split('-') #issue here
          checkinem.add_field(name=realm, value=emoji, inline=False)
        checkinmsg = await ctx.send(embed=checkinem)

        for channel in category.channels: 
          realm, emoji = channel.name.split('-') #issue here
          await checkinmsg.add_reaction(emoji=emoji)

任何帮助都会很好!谢谢大家!


Tags: nameidforherechannel频道域名ctx
1条回答
网友
1楼 · 发布于 2024-09-29 23:17:53

根据您的问题判断,您有两个可能的频道名称:

  • #领域表情符号
  • #域名表情符号

在这两种情况下,realm是第一个元素,emoji是最后一个元素,因此,您可以检查拆分的频道名称长度:

channel = channel.split('-')
if len(channel) == 2: # #real-emoji
    realm, emoji = channel
else: # #realm-name-emoji  
    realm, emoji = channel[0], channel[-1]

相关问题 更多 >

    热门问题