【问题标题】:Show the user who was banned discord.py显示被禁止的用户 discord.py
【发布时间】:2021-07-27 14:24:00
【问题描述】:

我有这个禁止垃圾邮件发送者的代码:

@bot.event
async def on_message(message):
    counter = 0
    with open("spam_detect.txt", "r+") as file:
        for lines in file:
            if lines.strip("\n") == str(message.author.id):
                counter += 1

        file.writelines(f"{str(message.author.id)}\n")
        if counter > 5:
            await message.guild.ban(message.author, reason="Spammer")
            await asyncio.sleep(5)
            await message.guild.unban(message.author)
            await message.channel.send("Banned for spam")
            print("banned")

用户被软禁,消息被删除,但我只能说Banned for spam..并且机器人擦除用户所做的一切,我不知道谁被禁止以及何时被禁止。我怎样才能说<USER> was banned for spam

【问题讨论】:

  • 像这样使用 f 字符串:f"{message.author} was banned for spam."

标签: python discord discord.py


【解决方案1】:

我通常不建议在on_message 事件中做这样的繁重的事情。 我建议使用缓存,并像往常一样添加用户。

但要回答你的问题:

if counter > 5:
    await message.guild.ban(message.author, reason="Spammer")
    await asyncio.sleep(5)
    await message.guild.unban(message.author)
    await message.channel.send(f"Banned {message.author.name} for spam")
    print("banned")

我添加了f"Banned {message.author.name} for spam"。这显示了被禁止用户的姓名。如果你想用鉴别器获取全名,请改用message.author

【讨论】:

    猜你喜欢
    • 2021-04-28
    • 2019-11-12
    • 2020-07-07
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 2019-07-20
    相关资源
    最近更新 更多