在 modmail 功能 [discord.py] 中让成员总是返回 None
Get member always returning None in modmail feature [discord.py]
我正在尝试在服务器中创建 Modmail 功能,该功能的作用:
1- 一旦用户 dms 机器人,就会创建一个 modmail 类别中的频道,频道名称与 dm 机器人的用户 ID 相同。
2- 之后用户发送给机器人的任何消息都会发送到 modmail 频道。
3- 在 modmail 频道中发送的任何消息都会直接发送给用户。
机器人通过频道名称知道将消息发送给谁的方式,因为频道名称等于打开票证的用户的 ID,但是,当机器人尝试获取具有该 ID 的成员时在频道名称中,它总是 returns None.
这是我的代码:
@bot.event
async def on_message(message):
guild = bot.get_guild(704414884187602994) ## Get the modmail guild
category = discord.utils.get(guild.categories, id=754287971347464223) ## Get the modmail category
overwrites = {
guild.default_role: discord.PermissionOverwrite(read_messages=False),
guild.me: discord.PermissionOverwrite(read_messages=True)
} ## Permissions for ticket channel
if isinstance(message.channel, DMChannel): ## Check if a user dms the bot
if message.author != bot.user: ## Doesn't make the bot respond to itself
channel = discord.utils.get(guild.channels, name=f"{message.author.id}") ## Ticket channel
if channel is None: ## Check if there is no open tickets for this user
await message.channel.send("Welcome to Modmail!") ## Sends message when user dms the bot
await category.create_text_channel(name=f"{message.author.id}", overwrites=overwrites) ## Create ticket channel if none found
elif channel is not None: ## If ticket found
await channel.send(f"Message recieved from {message.author.name}#{message.author.discriminator}, content: {message.content}") ## Send message to ticket channel
if message.channel.category == category: ## If a message is sent to the ticket channel
TheID = message.channel.name ## The ticket channel name which is the same name as the reporter ID
reporter = guild.get_member(TheID) ## Get the reporter to send messages to
print(TheID) ## Print the ID of the reporter
if reporter is None: ## If no user has the ID given in the channel name
print("Member not found.")
else:
await reporter.send(f"Message recieved from bot, content = {message.content}") ## Send message to reporter
任何有关此问题的帮助将不胜感激。
问题出在这一行TheID = message.channel.name
TextChannel.name
returns 一个作为频道名称的字符串,并且 get_member() 需要 ID,它应该是整数,所以你应该做 reporter = guild.get_member(int(TheID))
TheID = message.channel.name
reporter = guild.get_member(int(TheID))
print(TheID)
if reporter is None:
print("Member not found.")
else:
await reporter.send(f"Message recieved from bot, content = {message.content}")```
我正在尝试在服务器中创建 Modmail 功能,该功能的作用:
1- 一旦用户 dms 机器人,就会创建一个 modmail 类别中的频道,频道名称与 dm 机器人的用户 ID 相同。
2- 之后用户发送给机器人的任何消息都会发送到 modmail 频道。
3- 在 modmail 频道中发送的任何消息都会直接发送给用户。
机器人通过频道名称知道将消息发送给谁的方式,因为频道名称等于打开票证的用户的 ID,但是,当机器人尝试获取具有该 ID 的成员时在频道名称中,它总是 returns None.
这是我的代码:
@bot.event
async def on_message(message):
guild = bot.get_guild(704414884187602994) ## Get the modmail guild
category = discord.utils.get(guild.categories, id=754287971347464223) ## Get the modmail category
overwrites = {
guild.default_role: discord.PermissionOverwrite(read_messages=False),
guild.me: discord.PermissionOverwrite(read_messages=True)
} ## Permissions for ticket channel
if isinstance(message.channel, DMChannel): ## Check if a user dms the bot
if message.author != bot.user: ## Doesn't make the bot respond to itself
channel = discord.utils.get(guild.channels, name=f"{message.author.id}") ## Ticket channel
if channel is None: ## Check if there is no open tickets for this user
await message.channel.send("Welcome to Modmail!") ## Sends message when user dms the bot
await category.create_text_channel(name=f"{message.author.id}", overwrites=overwrites) ## Create ticket channel if none found
elif channel is not None: ## If ticket found
await channel.send(f"Message recieved from {message.author.name}#{message.author.discriminator}, content: {message.content}") ## Send message to ticket channel
if message.channel.category == category: ## If a message is sent to the ticket channel
TheID = message.channel.name ## The ticket channel name which is the same name as the reporter ID
reporter = guild.get_member(TheID) ## Get the reporter to send messages to
print(TheID) ## Print the ID of the reporter
if reporter is None: ## If no user has the ID given in the channel name
print("Member not found.")
else:
await reporter.send(f"Message recieved from bot, content = {message.content}") ## Send message to reporter
任何有关此问题的帮助将不胜感激。
问题出在这一行TheID = message.channel.name
TextChannel.name
returns 一个作为频道名称的字符串,并且 get_member() 需要 ID,它应该是整数,所以你应该做 reporter = guild.get_member(int(TheID))
TheID = message.channel.name
reporter = guild.get_member(int(TheID))
print(TheID)
if reporter is None:
print("Member not found.")
else:
await reporter.send(f"Message recieved from bot, content = {message.content}")```