【问题标题】:Pull a dynamic prefix into the status (discord.py)将动态前缀拉入状态(discord.py)
【发布时间】:2021-05-14 06:50:21
【问题描述】:

我目前正在处理我的状态,其中包含正在收听 .help。现在我想,当我使用命令 .setprefix 更改前缀动态时,前缀会更改,我的代码是:

@client.command()
async def setprefix(ctx, prefix):
    with open("./cogs/prefixes.json", "r") as f:
        prefixes = json.load(f)
    prefixes[str(ctx.guild.id)] = prefix
    with open("./cogs/prefixes.json", "w") as f:
        json.dump(prefixes, f, indent=4)

    await ctx.send(embed=discord.Embed(color=discord.Color.green(), title="Changed Prefix", description=f"Changed the prefix to **`{prefix}`**"))

我现在如何设置前缀以重新调整到新状态? 我的状态循环包含:

        while True:
            await self.client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.watching, name="the developers"))
            await asyncio.sleep(10)
            await self.client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.listening, name="to .help"))
            await asyncio.sleep(10)

为了使前缀动态化,我使用以下代码:

def get_prefix(client, message):
    with open("./cogs/prefixes.json", "r") as f:
        prefixes = json.load(f)
    return prefixes[str(message.guild.id)]

client = commands.Bot(command_prefix=get_prefix)

【问题讨论】:

  • 您的代码目前没有更改机器人的前缀,仅在 JSON 文件中,这是您想要的吗?
  • edit您的问题并在此处添加代码,而不是在 cmets 中
  • @ŁukaszKwieciński 我在帖子中添加了代码

标签: python json discord.py


【解决方案1】:

您的代码所做的是更改一个公会的前缀。 bot 显示的状态在每台服务器上都是相同的。

您可以尝试以下方法,但我不能向您保证,它会起作用。

with open('./cogs/prefixes.json', 'r') as f:
        prefixes = json.load(f)
    while True:
        await self.client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.watching, name="the developers"))
        await asyncio.sleep(10)
        await self.client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.listening, name="to prefixes[str(message.guild.id)]help")) # prefixes[str(message.guild.id)] is the prefix in your json file
        await asyncio.sleep(10)

【讨论】:

    猜你喜欢
    • 2015-03-15
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多