命令 SetPrefix 和 Json?不和谐

时间:2021-02-03 17:06:18

标签: python discord discord.py

首先: 我想为我的机器人添加更多功能,以便我创建一个新文件夹来尝试新事物!

第二: 我想添加一个命令来设置前缀!

第三: 我在 bot.py 旁边添加了一个 prefix.json 文件和一个 start.bat 来启动机器人

第四: 麻烦! 写脚本1280多点赞的时候,bat和python文件都没有加载,都关闭了

第五: 剧本!

import discord
import json
from discord.ext import commands

def get_prefix(bot, message):
    with open('prefix.json', 'r') as f:
        prefixes = json.load(f)

    return prefixes[str(guild.id)] = '.'

token = 'Private (-:'
bot = commands.Bot(command_prefix='c:', help_command=None)

@bot.event
async def on_ready():
    print('Ya estoy')

@bot.event
async def on_guild_join(guild):
    with open('prefix.json', 'r') as f:
        prefixes = json.load(f)

    prefixes[str(guild.id)] = '.'

    with open('prefix.json', 'w') as f:
        json.dump(prefixes, f, indent=4)

@bot.event
async def on_guild_remove(guild):
    with open('prefix.json', 'r') as f:
        prefixes = json.load(f)

    prefixes.pop(str(guild.id))

    with open('prefix.json', 'w') as f:
        json.dump(prefixes, f, indent=4)

@bot.command()
async def setprefix(ctx, prefix):
    with open('prefix.json', 'r') as f:
        prefixes = json.load(f)

    prefixes[str(ctx.guild.id)] = prefix

    with open('prefix.json', 'w') as f:
        json.dump(prefixes, f, indent=4)

bot.run(token)

1 个答案:

答案 0 :(得分:1)

第一:有什么问题?

第二:你的get_prefix函数需要传入Bot的command_prefix

代码:

def get_prefix(bot, message): 
    with open('prefix.json', 'r') as f:
        prefixes = json.load(f)

    return prefixes[str(message.guild.id)]

bot = commands.Bot(command_prefix=get_prefix, help_command=None)

第三:你的 bat 文件有什么问题?

第四:我不确定你的蝙蝠在做什么以及你用它做什么。