如何将机器人提及设置为机器人前缀? (不和谐.py)

时间:2021-06-18 11:49:10

标签: python discord.py bots prefix

我将我的机器人设置为能够拥有自定义前缀,现在我希望我的机器人能够在有人提到它时做出响应。当有人@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
    )

1 个答案:

答案 0 :(得分:0)

我明白了。


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)