[英]DM a specific user in discord.py
我想知道如何与一个永远相同的特定人联系。 我尝试了许多 StackOverFlow 帖子和官方 discord.py 文档,但都没有奏效。 我有 discord.py 1.7.3。 到目前为止,我有以下内容:
import discord
from discord.ext import commands
@client.command()
async def dm(ctx):
user = client.get_user(1234567891011) # <- This user will always be the same.
await user.send("Hi")
当我在服务器中键入命令时,会出现以下异常:
Ignoring exception in command dm:
Traceback (most recent call last):
File "C:\Users\myuser\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "c:\Users\myuser\Documents\Python Scripts\Bot\main.py", line 110, in dm await user.send("Hola")
AttributeError: 'NoneType' object has no attribute 'send'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\myuser\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\myuser\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\myuser\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'send'
client.get_user
方法在本地用户缓存上运行。 因此,除非您已经缓存了您想要 dm 的用户(例如通过与他们共享服务器并启用Intents.members
),否则您不会将它们放在本地缓存中,因此get_user
-method 将返回None
。
要让它独立于本地用户缓存工作,您可以改用client.fetch_user
。 它将通过用户的 id 从 discord API 中获取用户,因此总是返回一个用户对象(假设给定的 id 存在)。
使用client.fetch_user
,您的命令可能如下所示:
@bot.command()
async def dm(ctx):
user = await client.fetch_user(1234567891011)
await user.send('Hi')
此实现允许公会的任何成员在任何频道中简单地键入!dm
,并且机器人将向client.get_user(<user ID>)
指定的client.get_user(<user ID>)
发送消息
import discord
import os
from discord.ext import commands
# Declaration Discord.py Variables
intents = discord.Intents.default() # Turns on the connection
intents.members = True # Ensures the member list will be updated properly
client = commands.Bot(command_prefix='!', intents=intents) # defines the symbol used to call a command from the bot
@client.command()
async def dm(ctx):
"""
Sends a message to a user every time a member types '!dm' in the channel.
:param ctx: represents the context in which a command is being invoked under
:return: a message to the user
"""
await client.get_user(470163626083352577).send("Enter Text")
client.run("Bot_Token")
Bot.get_user()
在客户端的内部缓存中查找。 如果在缓存中找不到用户,则此方法返回None
。
请注意,如果get_user
返回 None,则该用户可能不与机器人共享公会,因此您无法 DM 该用户。
您可以选择尝试获取用户,但除非您与用户共享公会,否则您仍然无法 DM 用户。
user = await client.fetch_user(id)
await user.send('Message')
这将提高discord.NotFound
如果用户没有发现或discord.Forbidden
如果你不能DM用户。
确保 id 是整数。
出现的错误是因为您必须在发送消息之前在变量中创建一个 dm
试试这个:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='!')
@client.command()
async def dm(ctx,member:discord.Member,*,content):
channel = await member.create_dm() #create dm
await channel.send(content) #send the message
编辑:在这个新代码中,当您运行命令 !dm <@user> [message] 时,机器人将创建一个名为频道的新 dm(注意:频道名称只是一个变量,它不会命名 dm 频道)然后发送您上次添加的消息
注意:用户必须在您发送 dm 的服务器中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.