【问题标题】:Python Discord Bot allow links [duplicate]Python Discord Bot允许链接[重复]
【发布时间】:2021-04-06 18:20:59
【问题描述】:

我正在尝试制作一个机器人,它只允许链接和带有或不带文本的图像。 我编写的这段代码删除了我发布的每个链接,因为机器人将链接视为普通文本。 这是代码:

import discord

client = discord.Client()

@client.event
async def on_message(message):
    
    if str(message.channel) == "multimedia" and message.content != "" and not message.attachments:
        await message.channel.purge(limit=5)

client.run('token')

我想使用正则表达式,但我不知道如何以正确的方式编写它来告诉允许每个以 http 或 https 开头的链接

【问题讨论】:

    标签: python-3.x discord.py


    【解决方案1】:

    你可以只检查邮件是否以https://开头,并检查邮件是否有附件

    @client.event
    async def on_message(message):
          if message.author.bot:
             return #allow bots to send messages on the channel even if they don't include an image/URL
          if message.content.startswith("https://") or message.attachments != []:
             await message.channel.send("This is a link or an image!")
          else:
              await message.delete()
    

    【讨论】:

      猜你喜欢
      • 2021-10-16
      • 2020-07-13
      • 2023-01-23
      • 2021-02-16
      • 2020-06-10
      • 1970-01-01
      • 2021-05-03
      • 2019-10-19
      • 2019-05-17
      相关资源
      最近更新 更多