M
M
Mem13882021-02-14 23:50:58
Python
Mem1388, 2021-02-14 23:50:58

How to make multiple Bot.event on_message?

Tell me how to make several Bot.event with on_message (I want to filter the chat on the message, and the level system. The level system works fine, but the filtering does not work), if there is a second one, it displays the function is already defined problem

@Bot.event
async def on_message(message):
    if message.author.bot: return
    with open("D:\\PythonWorks\\DiscordAL\\UsersLevel.json", 'r') as f:
        users = json.load(f)
    async def userdata_update(users, user):
        if not user in users:
            users[user] = {}
            users[user]['User_Experience'] = 0
            users[user]['User_Level'] = 1
    async def UserAdd_experience(users, user, User_Experience):
        users[user]['User_Experience'] += User_Experience
    async def UserAdd_Level(users, user):
        User_Experience = users[user]['User_Experience']
        User_Level = users[user]['User_Level']
        if User_Experience > User_Level:
            User_Level = User_Level
            User_Experience = 0
            emb = discord.Embed(color = 0x00FF00)
            emb.set_author(icon_url=message.author.avatar_url, name = f'{message.author} Повышен уровень!')
            emb.add_field(name = 'Полученный уровень', value = User_Level + 1)
            emb.set_thumbnail(url='https://b.thumbs.redditmedia.com/oXMFXkldXGxJYKVeqxJkDq00R-vEYmMYEBDKD2Cbyls.png')
            await message.author.send(embed = emb)
            users[user]['User_Experience'] = 0
            users[user]['User_Level'] = User_Level + 1
    await userdata_update(users, str(message.author.id))
    await UserAdd_experience(users, str(message.author.id),0.1)
    await UserAdd_Level(users, str(message.author.id))
    with open("D:\\PythonWorks\\DiscordAL\\UsersLevel.json", 'w') as f:
        json.dump(users, f)
    await Bot.process_commands(message)

@Bot.event
async def on_message(message):
    if message.content.startswith("Привет"):
        await message.channel.send("Привет, мы тебя ждали!")

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladislav Mukhin, 2021-02-15
@Mem1388

you need to push everything into one "on_message"

M
Maxim Nevzorov, 2021-02-15
@fixator10

Use the decorator bot.listen
https://discordpy.readthedocs.io/en/stable/ext/com...
Or add functions for "listening" via bot.add_listener
https://discordpy.readthedocs.io/en/stable/ext/com...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question