首页 > 解决方案 > Discord Client Bot 没有以正确的方式响应?不和谐.py

问题描述

回答:

所以我从下面的问题中做了其他事情,实际上我没有使用.JSON文件并尝试了这个:

Prefixes.py(齿轮中的文件):

    @commands.Cog.listener()
    async def on_message(self, message):

        mention = f'<@!{self.client.user.id}>'
        if mention in message.content:
            prefix = await self.client.get_prefix(message)
            try:
                x = ""
                pfp = self.client.user.avatar_url
                prefix = discord.Embed(title=x, description=f"My prefix for **{message.guild.name}** is `{prefix}`. If you want to find out more information about me type `{prefix}help`.", color = 0x456383)
                prefix.set_footer(text=f"ChizLanks", icon_url=pfp)
                await message.channel.send(embed = prefix)
            except discord.Forbidden:
                return await message.channel.send(f"My prefix for **{message.guild.name}** is `{prefix}`. If you want to find out more information about me type `{prefix}help`")

我所做的是,我添加了prefix = await self.client.get_prefix(message). 这将从.JSON文件中获取前缀,或者如果.JSON文件为空,它将使用/可以在Main.py文件中找到的默认前缀。这对我来说很好,对你也应该很好。我在主文件中的内容可以在这里找到:

主要.py

def get_prefix(client, message):
    with open('prefixes.json', 'r') as file:
        prefixes = json.load(file)

    if str(message.guild.id) in prefixes:
        return prefixes[str(message.guild.id)]

    else:
        return "/"

client = commands.Bot(command_prefix = get_prefix)

==================================================== ==========================

问题:

所以我正在制作一个on_message事件,所以每当有人提到客户端的 ID 时,它都会回复一条带有服务器前缀的消息。(我正在使用一个per-server-prefix系统。)所以我尝试了这个:

    @commands.Cog.listener()
    async def on_message(self, message):


        def botprefix(self, message):
            with open('prefixes.json', 'r') as file:
                prefixes = json.load(file)

            if str(message.guild.id) in prefixes:
                return prefixes[str(message.guild.id)]

            else:
                return "/"

        mention = f'<@!{self.client.user.id}>'
        if mention in message.content:
            try:
                x = ""
                pfp = self.client.user.avatar_url
                prefix = discord.Embed(title=x, description=f"My prefix for **{message.guild.name}** is `{botprefix}`. If you want to find out more information about me type `{botprefix}help`.", color = 0x456383)
                prefix.set_footer(text=f"ChizLanks", icon_url=pfp)
                await message.channel.send(embed = prefix)
            except discord.Forbidden:
                return await message.channel.send(f"My prefix for **{message.guild.name}** is `{botprefix}`. If you want to find out more information about me type `{botprefix}help`")

但它似乎无法正常工作,每当我提到机器人时,它就会出现在此处输入图像描述

谁能解释为什么这不起作用以及我该如何解决?

标签: pythonpython-3.xdiscord.py

解决方案


您正在使用一个函数来生成前缀,这意味着您需要先调用它才能实际获取前缀。这是您需要进行的更改:

@commands.Cog.listener()
async def on_message(self, message):


    def botprefix(self, message):
        with open('prefixes.json', 'r') as file:
            prefixes = json.load(file)

        if str(message.guild.id) in prefixes:
            return prefixes[str(message.guild.id)]

        else:
            return "/"

    mention = f'<@!{self.client.user.id}>'
    if mention in message.content:
        prefix = botprefix(message) # Calculate the bot prefix for this guild
        try:
            x = ""
            pfp = self.client.user.avatar_url
            prefix = discord.Embed(title=x, description=f"My prefix for **{message.guild.name}** is `{prefix}`. If you want to find out more information about me type `{prefix}help`.", color = 0x456383)
            prefix.set_footer(text=f"ChizLanks", icon_url=pfp)
            await message.channel.send(embed = prefix)
        except discord.Forbidden:
            return await message.channel.send(f"My prefix for **{message.guild.name}** is `{prefix}`. If you want to find out more information about me type `{prefix}help`")

我存储了botprefix(message)inprefix然后在 f-strings 中使用它的结果。


读我

我上面所做的是对您的代码的快速而肮脏的修复。但是,您应该对您的机器人进行一些更改。首先,您应该botprefix()退出该on_message函数,然后在声明您的机器人时,将其用作command_prefix参数,如下所示:

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

    if str(message.guild.id) in prefixes:
        return prefixes[str(message.guild.id)]

    else:
        return "/"


bot = commands.Bot(command_prefix=get_prefix)

您可以在此处找到有关计算动态前缀的更多信息。


推荐阅读