【问题标题】:How do I set bot mention as a bot prefix? (discord.py)如何将机器人提及设置为机器人前缀? (discord.py)
【发布时间】:2021-09-03 03:50:51
【问题描述】:

我将我的机器人设置为能够具有自定义前缀,现在我希望我的机器人能够在有人提及它时做出响应。当有人 @bot@bot help 时,它都应该响应。

def get_prefix(client, message): 
    with open('prefixes.json', 'r') as f: ##we open and read the prefixes.json, assuming it's in the same file
        prefixes = json.load(f) #load the json as prefixes
    return prefixes[str(message.guild.id)] #recieve the prefix for the guild id given

client = commands.Bot(
    command_prefix= (get_prefix),
    intents = intents
    )

【问题讨论】:

    标签: python discord.py bots prefix


    【解决方案1】:

    我明白了。

    
    def get_prefix(client, message): 
        with open('prefixes.json', 'r') as f: ##we open and read the prefixes.json, assuming it's in the same file
            prefixes = json.load(f) #load the json as prefixes
        prefix = prefixes[str(message.guild.id)]
        return when_mentioned_or(*prefix)(client, message) #recieve the prefix for the guild id given
    
    client = commands.Bot(
        command_prefix= (get_prefix),
        intents = intents
        )
    
    @client.event
    async def on_message(message):
    
        mention = f'<@!{client.user.id}>'
        if message.content == mention:
            with open('prefixes.json', 'r') as f: ##we open and read the prefixes.json, assuming it's in the same file
             prefixes = json.load(f) #load the json as prefixes
            prefix = prefixes[str(message.guild.id)]
            await message.channel.send(f"You called? My prefix for this server is `{prefix}`. Admins can change it by using `{prefix}changeprefix <new prefix>`")
        await client.process_commands(message)
    

    【讨论】:

      【解决方案2】:

      好的,根据@Bruce21,我得到了您要问的内容,这是一个很长的方法,它不会像@bot help 那样响应。我有一个简单的方法。

      试试看:client = commands.Bot(command_prefix= when_mentioned_or(get_prefix), intents = intents)

      【讨论】:

      • 如果可行,请考虑将其标记为已接受的答案
      猜你喜欢
      • 2020-11-25
      • 1970-01-01
      • 2020-10-03
      • 1970-01-01
      • 2021-02-12
      • 1970-01-01
      • 1970-01-01
      • 2020-12-30
      • 1970-01-01
      相关资源
      最近更新 更多