python - Discord.py 警告命令未发送嵌入
问题描述
我正在尝试使用 discord.py 和 mongodb 作为数据库来发出警告命令。数据库用于存储惩罚并找到日志嵌入需要发送到的通道。
这是我当前的代码:
@commands.command()
@commands.has_permissions(manage_messages=True)
async def warn(self, ctx, member: discord.Member, *, reason=None):
ctx.logging = cluster["Logging"]["Punishments logs"]
self.channel = cluster["Logging"]["logchannels"]
embed = discord.Embed(title="\u200b", color=0x008000)
warn_embed = discord.Embed(
colour=discord.Color.magenta()
)
warn_embed.set_author(name='Punishment Notifcation')
warn_embed.add_field(name=f'You have been warned in **{ctx.guild.name}**', value=f'For `{reason}.`', inline=True)
if member == ctx.author:
await ctx.channel.send("You can't warn yourself :person_facepalming:")
return
elif reason == None:
reason = "No stated reason"
await member.send(embed=warn_embed)
await ctx.send(f'{member.mention} has been warned.')
print(f'{ctx.author} warned {member.mention} for {reason}')
ctx.logging.insert_one({"guild_id": ctx.guild.id, "action": "warn", "moderator": ctx.author.id, "user": member.id, "reason": reason})
channel = self.setup.find_one({"guild_id": ctx.guild.id})
if channel is not None:
elif_embed = discord.Embed(
title=f'Warn | Case (case number in future)',
color=discord.Color.magenta()
)
elif_embed.set_author(name=member, url=discord.Embed.Empty, icon_url=member.avatar_url)
elif_embed.add_field(name=f'**Offender:**', value=f'{member} {member.mention}')
elif_embed.add_field(name=f'**Reason:**', value=f'{reason}')
elif_embed.add_field(name='**Responsible Moderator:**', value=f'{ctx.author} {ctx.author.mention}')
await ctx.send(embed=elif_embed)
else:
await ctx.send('This command cannot be logged as moderation logs have not been enabled. To enable them do: **+setupmod <channel>**')
else:
await member.send(embed=warn_embed)
await ctx.send(f'{member.mention} has been warned by {ctx.author.mention}')
print(f'{ctx.author} warned {member} for {reason}')
ctx.logging.insert_one({"guild_id": ctx.guild.id, "action": "warn", "moderator": ctx.author.id, "user": member.id, "reason": reason})
channel = self.setup.find_one({"guild_id": ctx.guild.id})
if channel is not None:
else_embed = discord.Embed(
title=f'Warn | Case (case number in future)',
color=discord.Color.magenta()
)
else_embed.set_author(name=member, url=discord.Embed.Empty, icon_url=member.avatar_url)
else_embed.add_field(name=f'**Offender:**', value=f'{member} {member.mention}')
else_embed.add_field(name=f'**Reason:**', value=f'`{reason}`')
else_embed.add_field(name='**Responsible Moderator:**', value=f'{ctx.author} {ctx.author.mention}')
await ctx.send(embed=else_embed)
else:
await ctx.send('This command cannot be logged as moderation logs have not been enabled. To enable them do: **+setupmod <channel>**')
预期的结果是:在命令运行的通道中,它会发送一条消息确认它有效,它会通知用户他们已被警告,如果已设置日志通道,它会向该通道发送嵌入
实际结果是前两部分工作,但是嵌入没有发送到正确的通道(它已被设置)并且我收到此错误:
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\name\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "c:\Users\name\Documents\Discord Bot\Testcode.py", line 98, in on_message
guild = setup.find_one({"guild_id": message.guild.id})
AttributeError: 'NoneType' object has no attribute 'id'
Discord Username #1234 warned <@!759497745886085132> for No stated reason
Ignoring exception in on_command_error
Traceback (most recent call last):
File "C:\Users\name\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\name\Documents\Discord Bot\cogs\moderation.py", line 100, in warn
channel = self.setup.find_one({"guild_id": ctx.guild.id})
AttributeError: 'Moderation' object has no attribute 'setup'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\name\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "c:\Users\name\Documents\Discord Bot\Testcode.py", line 231, in on_command_error
raise error
File "C:\Users\name\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\name\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\name\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: 'Moderation' object has no attribute 'setup'
我曾尝试使用打印命令来查找错误,但这并没有奏效,我还尝试使用人们无法帮助解决此问题的不和谐支持服务器。最重要的是,我查看了我的一些工作代码和 discord.py 文档,并没有发现在这种情况下可以提供帮助。
对此解决方案的任何帮助将不胜感激!
注意:如果有人了解 mongodb,您能否提出一种方法,我可以制作一个为每个惩罚分配一个 ID 的系统。谢谢
编辑: 这是错误的第一部分所指的代码部分:
@bot.event
async def on_message(message):
setup = cluster["Atomic_Developer_Bot"]["prefix"]
guild = setup.find_one({"guild_id": message.guild.id})
prefix = guild["prefix"]
if message.content.startswith('<@850794452364951554>'):
await message.channel.send(f'The current prefix is **{prefix}**')
else:
print(prefix)
await bot.process_commands(message)
解决方案
根据您的评论,我认为用户通知(通过 dm)是 on_message 中使用的通知。尝试这个:
@bot.event
async def on_message(message):
if message.author.id != your_bot_id_here:
setup = cluster["Atomic_Developer_Bot"]["prefix"]
guild = setup.find_one({"guild_id": message.guild.id})
prefix = guild["prefix"]
if message.content.startswith('<@850794452364951554>'):
await message.channel.send(f'The current prefix is **{prefix}**')
else:
print(prefix)
await bot.process_commands(message)
推荐阅读
- encoding - 在不构建霍夫曼树的情况下生成前缀代码列表
- mysql - MySQL 集群是否支持协调节点?
- firebase - GeneratedPluginConstraint,致命错误:找不到模块“firebase_auth”@import firebase_auth;
- machine-learning - jupyter notebook:运行单元不起作用
- javascript - rxfire 订阅未在 componentdidmount 中触发
- json - 条件jq提取层次结构不同位置的两个元素
- go - 在绑定到特定的 127.xxx 地址时侦听多播环回
- html - 我试图让一个按钮转到另一个页面,但它不起作用
- python - 将字节字符串拆分为列
- azure - 是否可以使用站点到站点 (VPN) 连接来连接两个 Azure VNet?