Home > OS >  give bot.listen a name , to call the name for later use . discord.py
give bot.listen a name , to call the name for later use . discord.py

Time:06-11

currently iam creating a log embed . and i want give a bot.listen event a name. and use it as Embed(title=f'{the name}' so i can use the same embed i build for multiple listen events . for example :

#bot.py
from http import client
import discord
import asyncio
from discord.ext import commands
import os
from dotenv import load_dotenv
from datetime import datetime, timedelta, date, time, timezone
import time
import loging
load_dotenv()

TOKEN = os.getenv("DISCORD_TOKEN")

bot = commands.Bot(command_prefix=commands.when_mentioned_or("$"), help_command=None)
timestamp = datetime.now()
timenow = str(timestamp.strftime(r"%x, %X"))

@bot.listen(name='complexity')#give the name i want show as tittle 
async def on_message(message):
    if any(x in message.content.lower() for x in ["complexity", "complex"]):
        await message.add_reaction(r":complexity:945474998208958517")
        await loging.meslog(bot, message)

bot.run(TOKEN)
#loging.py
from http import client
import discord
import asyncio
from discord.ext import commands
import os
from datetime import datetime, timedelta, date, time, timezone
import time

timestamp = datetime.now()
timenow = str(timestamp.strftime(r"%x, %X"))

async def meslog(bot, message):
    username = (message.author)
    usernameid = str(message.author.id)
    messagein = str(message.content)
    messageid = str(message.id)
    if message.guild:
        channel = str(message.channel.name)
        channelid = str(message.channel.id)
        channelping = str(message.channel.mention)
    logingchan = await bot.fetch_channel(983811124929630239)
    em = discord.Embed(title=f'{message.name}', description=f'{timenow}', color=0x00FF00)
#{message.name} would output the name of the listen event.
#in this case Complexity
    em.set_thumbnail(url=username.avatar_url)
    em.add_field(name="Channel:", value=f'{channelping} \n{channelid}', inline=True)
    em.add_field(name="User:", value=f'{username}\n{usernameid}', inline=True)
    em.add_field(name="Message:", value=f'{messagein}\n{messageid}', inline=False)
    await logingchan.send(embed=em)

i know its worng and dont work , but is there any way to do waht i mean to do ? what is the correct way pls teach me.

CodePudding user response:

The listen decorator does not support custom names. It must be one of the actual events, like on_message.

You probably have it backwards:

@bot.listen(name='on_message')
async def complexity(message):
    if any(x in message.content.lower() for x in ["complexity", "complex"]):
        await message.add_reaction(r":complexity:945474998208958517")
        await loging.meslog(bot, message, 'complexity')

If you want to include the listener that called this, simply pass that as an argument:

async def meslog(bot, message, logger_name: str):
    # ...
    em = discord.Embed(title=f'{logger_name}', description=f'{timenow}', color=0x00FF00)
    # ...

If you want to be really fancy, you can get the caller function's name (requires 3.5 ) from meslog, but I personally don't recommend this:

async def meslog(bot, message, logger_name: str):
    # ...
    em = discord.Embed(title=f'{inspect.stack()[1].function}', description=f'{timenow}', color=0x00FF00)
    # this will return 'complexity' but you can name it whatever
    # ...
  • Related