首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用cogs实现change prefix命令?

如何使用cogs实现change prefix命令?
EN

Stack Overflow用户
提问于 2020-10-19 19:14:29
回答 1查看 239关注 0票数 0

首先,这是我的bot目录。

现在,这是所有用于简化前缀更改的事件和命令的外观。

代码语言:javascript
复制
def get_prefix(client, message):
    with open("./prefixes_data.json", "r") as f:
        prefixes = json.load(f)

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

client = commands.Bot(command_prefix = get_prefix)

@client.event
async def on_ready():
    print('bot ready hai')

@client.event
async def on_guild_join(guild):
    with open("./prefixes_data.json", "r") as f:
        prefixes = json.load(f)
    
    prefixes[str(guild.id)] = "!" # default value, implemented when bot joins for the first time

    with open("./prefixes_data.json", "w") as f:
        json.dump(prefixes, f, indent=4)

@client.event
async def on_guild_remove(guild):
    with open("./prefixes_data.json", "r") as f:
        prefixes = json.load(f)

    prefixes.pop(str(guild.id))

    with open("./prefixes_data.json", "w") as f:
        json.dump(prefixes, f, indent=4)

命令

代码语言:javascript
复制
@client.command()
async def changeprefix(ctx, new_prefix):
    with open("./prefixes_data.json", "r") as f:
        prefixes = json.load(f)
        
    prefixes[str(ctx.guild.id)] = new_prefix

    with open("./prefixes_data.json", "w") as f:
        json.dump(prefixes, f, indent=4)

    ctx.send(f"Changed the prefix to: {new_prefix}")

现在,我想这样做,但使用齿轮(具体地说,机器人节制齿轮)。但我没有得到想要的东西。请帮帮忙。请。这是我尝试过的齿轮代码。

代码语言:javascript
复制
import discord
from discord.ext import commands
import json

class botmodcoms(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.command
    async def changeprefix(self, ctx, new_prefix):
        with open("../prefixes_data.json", "r") as f:
            prefixes = json.load(f)

        prefixes[str(ctx.guild.id)] = new_prefix
        
        with open("../prefixes_data.json", "w") as f:
            json.dump(prefixes, f, indent=4)

def setup(client):
    client.add_cog(botmodcoms(client))

我想要的是所有事件都应该在主bot.py文件中,并且命令在cog文件(BotModeration)下。

EN

回答 1

Stack Overflow用户

发布于 2020-12-20 20:19:32

我似乎让它起作用了。

代码语言:javascript
复制
import discord
from discord.ext import commands
import json

class Config(commands.Cog, name="Configuration"):
        
        def __init__(self, bot):
            self.bot = bot
            
        @commands.Cog.listener()
        async def on_ready(self):
            print(f"{self.__class__.__name__} Cog has been loaded\n-----")
            
        @commands.command()
        async def prefix(self, ctx, new_prefix):
                  with open("./prefixes.json", "r") as f:
                    prefixes = json.load(f)
                    
                    prefixes[str(ctx.guild.id)] = new_prefix
                    
                    with open("./prefixes.json", "w") as f:
                        json.dump(prefixes, f, indent=4)
            
def setup(bot):
    bot.add_cog(Config(bot))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64426169

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档