程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在 discord.py 中获取 DM 消息大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决在 discord.py 中获取 DM 消息?

开发过程中遇到在 discord.py 中获取 DM 消息的问题如何解决?下面主要结合日常开发的经验,给出你关于在 discord.py 中获取 DM 消息的解决方法建议,希望对你解决在 discord.py 中获取 DM 消息有所启发或帮助;

所以我有这个代

import discord
from discord.ext.commands import Bot
from discord import DMChAnnel
TOKEN = "bot token"
clIEnt = Bot (command_prefix='!')
@clIEnt.command(name='dm',pass_context=TruE)
async def dmsend(ctX):
    user = await clIEnt.fetch_user("user ID")
    await DMChAnnel.send(user,"Hello there!")
clIEnt.run(TOKEN)

如果用户写了 !dm,它基本上会向特定用户发送一条消息。但是有没有办法通过 dms 做到这一点?因此,每次我在 dms 下收到带有 !dm 的消息时,它都会在 #general 频道中执行吗?

解决方法

您可以在某些地方添加一些约束并使代码更简单。

首先:使用 @commands.dm_only() 从而确保或仅允许在私人消息中执行该命令。

第二:您查询消息作者的方式有点复杂,不应为此使用 fetch,否则您可能会受到速率限制。最好的方法是使用以下内容:user = ctx.author

第三:要定义一个 general 频道或其他东西,您可以使用 client.get_chAnnel(ChAnnelID)

查看完整代码:

@client.command()
@commands.dm_only() # Can only be used in the bots DMs
async def dmsend(ctX):
    user = ctx.author
    await user.send("Hello there!") # Send a DM to the author of the command
    general = client.get_chAnnel(ChAnnelID) # Define the general chAnnel
    await general.send(f"Sent a DM to {Ctx.author}") # Your text after a DM

另请参阅 docs 了解更多信息

大佬总结

以上是大佬教程为你收集整理的在 discord.py 中获取 DM 消息全部内容,希望文章能够帮你解决在 discord.py 中获取 DM 消息所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。