python-3.x - Discord Bot python 3.6警告命令
问题描述
我一直在开发一个主持人不和谐机器人。做了除警告命令之外的所有命令。谁能帮我发出警告命令。
如果成员(具有管理成员权限)键入?warn @user reason
,机器人会将警告保存在 .json 文件中。
如果用户说?warnings @user
机器人将显示用户的警告。
解决方案
你可以做这样的事情
import discord
from discord.ext.commands import commands,has_permissions, MissingPermissions
import json
with open('reports.json', encoding='utf-8') as f:
try:
report = json.load(f)
except ValueError:
report = {}
report['users'] = []
client = discord.ext.commands.Bot(command_prefix = '?')
@client.command(pass_context = True)
@has_permissions(manage_roles=True, ban_members=True)
async def warn(ctx,user:discord.User,*reason:str):
if not reason:
await client.say("Please provide a reason")
return
reason = ' '.join(reason)
for current_user in report['users']:
if current_user['name'] == user.name:
current_user['reasons'].append(reason)
break
else:
report['users'].append({
'name':user.name,
'reasons': [reason,]
})
with open('reports.json','w+') as f:
json.dump(report,f)
@client.command(pass_context = True)
async def warnings(ctx,user:discord.User):
for current_user in report['users']:
if user.name == current_user['name']:
await client.say(f"{user.name} has been reported {len(current_user['reasons'])} times : {','.join(current_user['reasons'])}")
break
else:
await client.say(f"{user.name} has never been reported")
@warn.error
async def kick_error(error, ctx):
if isinstance(error, MissingPermissions):
text = "Sorry {}, you do not have permissions to do that!".format(ctx.message.author)
await client.send_message(ctx.message.channel, text)
client.run("BOT_TOKEN")
您将所有用户的报告保存在一个名为的文件中reports.json
,而不是manage_roles=True, ban_members=True
在其中,@has_permissions
您可以将文档中的任何内容
推荐阅读
- apache-spark - Pyspark 在返回 0 的双精度数上转换整数
- visual-studio-code - vscode如何让“打字屏”变大
- amazon-web-services - FFMpeg WebVTT hls 播放列表不同步
- python - Traceback(最近一次调用最后一次):文件“/var/www/html/tki.py”,_tkinter.TclError:没有显示名称
- r - 为什么 R 中的 HTML 函数实际上不会编写 HTML?
- json - 我如何将字段 1 值作为 gson 的整数变量?
- reactjs - 无法访问“此”
- c - 如何在 C 中处理不同的验证问题集?
- python - pip install scipy 的问题 - RuntimeError: Broken toolchain: cannot link a simple C program
- c++ - 为什么我会收到类函数参数的错误?“没有匹配的调用函数”C++