簡體   English   中英

我正在嘗試創建 function,當有人在消息上發送鏈接時,它將被刪除

[英]I'm trying to create function, when somebody will send link on message, it will be deleted

這就是它應該如何工作的方式。 如果用戶在消息上發送鏈接,它將被刪除。 但如果成員具有特殊角色,則不會被刪除。 但它發送此錯誤:

line 154, in on_message
    if member.role.name == ('Test'):
NameError: name 'member' is not defineddefined

請幫我。

我的代碼:

async def on_message(message):
  url= re.findall('https//')
  if member.role.name == ('Test'):
    return
  elif url and message.channel.id == "842466772568899648":
    await bot.delete_message(message)

您的代碼中有一些錯誤:

  1. 您不在on_message事件中使用member 相反,您使用message.author
  2. 您可以獲得role對象的列表,因此檢查用戶是否具有角色的最佳方法是使用List Comprehension
  3. 每個 id 都是int而不是str所以你必須從"842466772568899648"中刪除"
  4. bot沒有名為delete_message()的方法,但message有方法delete()

它應該看起來如何:

@bot.event
async def on_message(message):
    if "Test" in [role.name for role in message.author.roles]:
        return
    if "https://" in message.content and message.channel.id == 842466772568899648:
        await message.delete()

您可以使用正則表達式檢查它。 也使用 message.author 而不是 member。

import re
REGEX = re.compile(r"https?://(?:www\.)?.+")

@bot.event
async def on_message(message):
    if not message.author.bot:
        if REGEX.match(message.content):
            await ctx.send("Please don't use any links in this channel!")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM