【问题标题】:Discord Bot python 3.6 warn commandDiscord Bot python 3.6警告命令
【发布时间】:2019-03-07 09:54:48
【问题描述】:

我一直在开发一个主持人不和谐机器人。做了除警告命令之外的所有命令。谁能帮我发出警告命令。

如果成员(具有管理成员权限)键入 ?warn @user reason,机器人会将警告保存在 .json 文件中。

如果用户说?warnings @user,机器人将显示用户的警告。

【问题讨论】:

  • 请花一些时间阅读如何提问以及如何创建complete and verifiable 示例。也许你最好也阅读how to ask
  • 您在为哪一部分苦苦挣扎?你是在问如何写文件,如何读文件,如何从命令参数中识别用户……?
  • 我很乐意帮助您解决这个问题,但是没有足够的信息可以提供帮助。你也从来没有问过问题,你只是陈述了你想让命令做的事情。

标签: python-3.x discord discord.py


【解决方案1】:

你可以这样做

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 中,您可以将 the documentation 中的任何内容放入其中

【讨论】:

  • 值得注意的是,这将在您每次重新启动机器人时清除 reports.json。当机器人启动时,应该有一些机制将report 初始化为等于现有的 json。这也消除了在调用 warnings 时需要 load 文件
  • @Tristo 如何添加另一个命令来删除或删除该警告。
  • @PatrickHaugh 如何通过另一个命令删除该用户报告。
  • 有没有改写友好的版本
猜你喜欢
  • 2020-08-11
  • 1970-01-01
  • 2018-10-21
  • 2021-03-01
  • 2018-07-08
  • 2021-04-11
  • 2021-07-03
  • 2018-07-07
  • 2021-02-17
相关资源
最近更新 更多