问题
Hi I'm trying to get a member name rather than the author name
I've tried a few methods like for member is message.server.members: which returned multiple results with every member in the server and tried member: discord.Member as a signature which produced an error:
Heres what I'm working with:
async def on_message_delete(self, message):
server = message.server
author = message.author
role = get(server.roles, name="Powerbot")
channel = get(message.server.channels, name="mod-log")
time = datetime.utcnow()
cleanmsg = message.content
for i in message.mentions:
cleanmsg = cleanmsg.replace(i.mention, str(i))
fmt = '%H:%M:%S'
name = author
name = " ~ ".join((name.name, name.nick)) if name.nick else name.name
if role not in author.roles:
infomessage = "A message by {}, was deleted in {} by {}".format(message.author.mention, message.channel.mention, member,mention)
delmessage = discord.Embed(description=infomessage, colour=discord.Color.purple(), timestamp=time)
delmessage.add_field(name="Message:", value=cleanmsg)
delmessage.set_footer(text="User ID: {}".format(message.author.id))
delmessage.set_author(name=name + " message deleted.", icon_url=message.author.avatar_url)
delmessage.set_thumbnail(url="http://i.imgur.com/fJpAFgN.png")
try:
await self.bot.send_message(channel, embed=delmessage)
except:
pass
The line specifically where member.mention is.
infomessage = "A message by {}, was deleted in {} by {}".format(message.author.mention, message.channel.mention, member.mention)
Example output: A message by author was deleted in channel by member.
If anyone could help that would be appreciated.
回答1:
Viewing the person who deleted the message is not possible from the discord release that you’re using, since the stable discord.py does not contain support for audit logs. You will need discord.py rewrite to use the following solution:
@bot.event()
async def on_message_delete(msg):
audits = await msg.guild.audit_logs(limit=10, action=discord.AuditLogAction.message_delete)
async for audit in audits:
try:
await audit.target.get_message(msg.id)
except discord.NotFound:
continue
print(audit.user)
break
That’s under the assumption that messages are kept even after being deleted. If the above does not work, I guess the best we can do is:
@bot.event()
async def on_message_delete(msg):
audits = await msg.guild.audit_logs(limit=10, action=discord.AuditLogAction.message_delete)
audit = await audits.get(extra__channel=msg.channel)
print(audit.user)
For further information, see:
https://discordapp.com/developers/docs/resources/audit-log
https://discordpy.readthedocs.io/en/rewrite/api.html#discord.AuditLogAction
回答2:
I'm pretty sure that the client isn't informed about who deleted the message, just the fact that it was deleted. The message object itself doesn't hold that information, and the RawMessageDeleteEvent from the rewrite branch doesn't have it either.
I'm not super familiar with the underlying Discord API, but the Message Delete gateway event looks to be what discord.py receives when a message is deleted, and it doesn't mention anything about the identity of the deleter.
来源:https://stackoverflow.com/questions/52527482/positional-error-when-trying-to-specify-member