I was creating a logging bot and I have use a single event to find what happened. Before completing my work I realized that it is not good for my bot to do such a long processing. I have unique set of code for every if statement I am not understanding how can I make it faster. i know i can use dictionary, functions, OOP. So what should I do now?
#🔒 How should I rewrite my endless if statement?
16 messages · Page 1 of 1 (latest)
@polar tide
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
!paste
Pasting large amounts of code
If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.
Just dont log it
No need for bots that logs everything
Audit log is best way
You cant delete it, edit
ok but i need know how to solve this problem if I encounter it in some day
in future
Move the common code to before or after your if statements, for example ```py
log_channel_id = await bot_db.get_log_channel(entry.guild.id)
log_channel = entry.guild.get_channel(log_channel_id)
embd = discord.Embed(colour=discord.Colour.from_str("#8F00FF")).set_footer(text=self.bot.user, icon_url=self.bot.user.avatar.url)
embd.timestamp = datetime.datetime.now(datetime.UTC)
embd.add_field(name="Name", value=entry.user, inline=False)
embd.add_field(name="Creation Date", value=f"{entry.created_at} UTC", inline=False)
embd.add_field(name="ID", value=f"User = {entry.user.id}\nChannel = {entry.target.id}")
if entry.action == discord.AuditLogAction.channel_create:
embd.description = f"{entry.after.type} channel created {entry.target.name}({entry.target.mention})"
embd.add_field(name="Type", value=entry.after.type, inline=False)
elif entry.action == discord.AuditLogAction.channel_delete:
embd.description = f"{entry.before.type} channel deleted #{entry.before.name}"
embd.add_field(name="Type", value=entry.before.type, inline=False)
await log_channel.send(embed=embd)
as for the "Changes", use a for loop
something like ```py
Name = {getattr(entry.before, "name", channel.name)} -> {getattr(entry.after, "name", channel.name)}\n
fields = ["name", "topic", ...]
changes = []
for field in fields:
before = getattr(entry.before, field, getattr(channel, field))
after = getattr(entry.after, field, getattr(channel, field))
# maybe if before != after:
changes.push((field, before, after))
desc = '\n'.join(f"{field.title()} = {before} -> {after}" for field, before, after in changes)
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.