【发布时间】:2021-04-23 21:45:48
【问题描述】:
我在 discord.py 中创建了一个警告系统,并且我得到了要写入所需信息的实际数据,但是当我再次运行该命令时,它只会替换原始对象/警告中的数据。关键是每个警告都被视为数组中的一个对象,但它不会为新警告添加另一个对象
@commands.command()
@commands.guild_only()
# @commands.has_guild_permissions(kick_members=True)
async def warn(self, ctx, user: discord.User, *, reason=None):
if user is None:
await ctx.send("You have not provided a user!")
return
if user is user.bot:
await ctx.send(f"<@{ctx.author.id}, you cannot warn a bot!")
return
if user.id == ctx.author.id:
embed = discord.Embed(description=f"<@{ctx.author.id}>, you are not allowed to warn yourself!")
embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar_url)
embed.set_footer(text=f"{ctx.message.guild.name} Custom Designed Bot")
embed.timestamp = ctx.message.created_at
await ctx.send(embed=embed)
return
reason = reason or None
_id = await self.bot.warns.find_by_id({"_id": user.id})
first_warning = True
warn = {
"_id": user.id,
"guild": ctx.message.guild.id,
"warn": [{
"PunishType": 'warn',
"Moderator": ctx.message.author.id,
"Reason": reason,
}]
}
if first_warning:
embed = discord.Embed(description=f"<@{ctx.author.id}>, has warned <@{user.id}> for {reason}")
embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar_url)
embed.timestamp = ctx.message.created_at
await ctx.send(embed=embed)
await self.bot.warns.upsert(warn)
return first_warning is False
elif first_warning is False:
warns = await self.bot.warns.find_by_id({"_id": user.id})["warns"]
newwarn = [{
"PunishType": "warn",
"Moderator": ctx.author.id,
"Reason": reason,
}]
warns.append(newwarn)
await self.bot.warns.update(
{"_id": "user.id"},
{"$push": {"warns": warns}})
【问题讨论】:
标签: arrays python-3.x mongodb discord.py-rewrite