Discord.py 不发送 dm?

问题描述

#MassDM Command
@bot.command()
async def massdm(ctx,msg):
    await ctx.message.delete()
    show_cursor()
    wipeinput = input(Fore.RED+"Are you sure you want to mass dm? (WARNING: This happens really fast so it will probably flag your account)(y or n): ")
    hide_cursor()
    if wipeinput == "y" or wipeinput == "Y":
        for user in ctx.guild.members:
            if user != bot.user:
                try:
                    channel = await user.create_dm()
                    await channel.send(msg)
                    print(Fore.GREEN + f'Sent to user "{user.name}"')
                except:
                    print(Fore.YELLOW + f'Failed to DM user "{user.name}"')
                    pass
        print(Fore.GREEN+"Finished")

当我运行它时,它只是说“已完成”而没有做任何事情。当我删除 try/except 时,它不会出错?我认为我已经设置了所有正确的意图

解决方法

您在这一行中犯了一个错误:

wipeinput = input(Fore.RED+"Are you sure you want to mass dm? (WARNING: This happens really fast so it will probably flag your account)(y or n): ")

您似乎在等待回复。这样做的方法是使用 wait_for 函数。你应该把它改成这样:

await ctx.send("Are you sure you want to mass dm? (WARNING: This happens really fast so it will probably flag your account)(y or n)?")

wipeinput = bot.wait_for('message')  # You can add another parameter to check if the input is valid and what you want.

此外,我不确定函数 show_cursor()hide_cursor() 是什么。它们也可能导致某些东西无法正常工作。

编辑:(感谢 IPSDSILVA 指出这一点)尽管我提供的代码并未围绕帖子中的问题展开,但代码可能会导致您的代码无法运行