【问题标题】:how do i add prefixes from when the bot was offline. discord.py如何从机器人离线时添加前缀。不和谐.py
【发布时间】:2020-08-03 22:14:11
【问题描述】:

每次机器人离线并且有人添加机器人时,机器人不会自动添加机器人离线时的前缀。我不知道如何构建一些东西来自动添加它,有什么想法吗?

我得到这个错误:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 930, in on_message
    await self.process_commands(message)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 926, in process_commands
    ctx = await self.get_context(message)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 842, in get_context
    prefix = await self.get_prefix(message)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 787, in get_prefix
    ret = await discord.utils.maybe_coroutine(prefix, self, message)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\utils.py", line 317, in maybe_coroutine
    value = f(*args, **kwargs)
  File "C:\Users\Administrator\Desktop\doob\bot.py", line 16, in get_prefix
    return prefixes[str(message.guild.id)]
KeyError: '{insert server id here}

(前缀.py):

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

    # Opens json file then dumps '-'
    @commands.Cog.listener()
    async def on_guild_join(self, guild):
        with open('prefixes.json', 'r') as f:
            prefixes = json.load(f)

        prefixes[str(guild.id)] = "-"

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

    # Removes guild from json file when Doob leaves.
    @commands.Cog.listener()
    async def on_guild_remove(self, guild):
        with open('prefixes.json', 'r') as f:
            prefixes = json.load(f)

        prefixes.pop(str(guild.id))

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

    # Changes the prefix (that the user provides.) for the specific server.
    @commands.command(aliases=['prefix'])
    @commands.has_permissions(administrator=True)
    async def changeprefix(self, ctx, prefix):
        with open('prefixes.json', 'r') as f:
            prefixes = json.load(f)

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

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

        embed = discord.Embed(title="An administrator has changed the prefix.", description=f"An administrator has changed the prefix to {prefix}.", colour=discord.Color.blue())

        embed.add_field(name="The prefix has been changed to:", value=prefix)
        embed.set_thumbnail(url=doob_logo)
        await ctx.send(embed=embed)

(bot.py):

# Creates and loads the json file.
def get_prefix(client, message):
    with open('prefixes.json', 'r') as f:
        prefixes = json.load(f)

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

client = commands.Bot(command_prefix = get_prefix)

有人要我的代码,所以这是我的代码,我仍然需要帮助,所以任何事情都会有所帮助!提前谢谢!!!

【问题讨论】:

  • 请出示您的代码
  • @EthanM-H 我已经编辑了帖子

标签: python discord discord.py offline prefixes


【解决方案1】:

我个人会采取不同的做法,而不是为每个新公会设置一个前缀,只为设置了它的公会自定义前缀以避免这种情况。像这样的东西会做你现在做的事情,除了它应该处理所有公会,因为它是后备/默认前缀

def get_prefix(client, message):
    with open('prefixes.json', 'r') as f:
        prefixes = json.load(f)
    try:
         return prefixes[str(message.guild.id)]
    except KeyError:
         return '-

【讨论】:

    猜你喜欢
    • 2021-05-19
    • 2017-10-15
    • 2021-06-19
    • 2020-06-20
    • 2021-07-07
    • 2020-12-18
    • 2020-10-29
    • 2022-01-21
    • 2021-06-25
    相关资源
    最近更新 更多