【问题标题】:Sending a dm when banning someone discord.py禁止某人时发送 dm discord.py
【发布时间】:2021-05-05 21:40:01
【问题描述】:

我有一个不和谐的垃圾邮件踢机器人,我想在你被禁止时发送私人消息。我试过这个:

@bot.event
async def on_message(message, ctx):
    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 > 4 and not message.author.guild_permissions.administrator:
            await message.guild.ban(message.author, reason="Spammer")
            await message.channel.send(f"{message.author} kicked for spam.")
         ***ctx.author.send(message.author, "You were kicked for spam. You can rejoin in`60`seconds")***
            await asyncio.sleep(30)
            await message.guild.unban(message.author)
            print(f"{message.author} kicked for spam.")

但它不起作用TypeError: on_message() missing 1 required positional argument: 'ctx'

问题是它也破坏了其余代码..所以现在根本不起作用。

关于如何向被禁止的人发送 dm有什么想法

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    您必须删除ctx 参数,因为在调用on_message 时它不会被传递,而不是ctx.author.send() 使用message.author.send()。如果你真的想在on_message 中使用ctx,因为你需要它用于代码的其他部分,因为它与问题无关,所以在on_message 的顶部添加ctx = await bot.get_context(message)


    参考资料:

    【讨论】:

    • 我现在收到这个错误:TypeError: send() takes from 1 to 2 positional arguments but 3 were given..这可能是因为(message.author, 吗?
    • 你只需要使用message.author.send("You were kicked for spam. You can rejoin in 60 seconds")@MihaiCristian
    猜你喜欢
    • 2021-04-16
    • 2022-01-24
    • 2021-03-03
    • 2022-01-11
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    相关资源
    最近更新 更多