【发布时间】:2021-10-20 15:18:34
【问题描述】:
我正在尝试使用 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)
【问题讨论】:
-
您没有将代码部分发送给我们,导致出现错误
-
对不起!我现在就编辑它
-
你能展示一下 setup.find_one 函数吗?
-
消息是通过 DM 发送的吗?
-
@yotamrec 这是我在问题中提出的主要代码块。我刚刚编辑的部分在
testcode.py中,如果有帮助的话,原始部分在moderation.py中! - 是的 dms 中的消息发送正确
标签: python mongodb discord.py