#πŸ”’ discord bot - logging error.

125 messages Β· Page 1 of 1 (latest)

little locust
#

Heya! Im currently working on a bot everything is working, but at a command, whenever someone that does not have the permission to do such, it should send the message said in the image. but it does not. If I do have the permission the other embed sends just fine, its not only on this line, its this one and another, please help me. Thanks.

brave vesselBOT
#

@little locust

Python help channel opened

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.

little locust
#

this is the second.

#

its the same but just different commands

#

i got stuck here

#

for the last hour

#

code : py @commands.command() @commands.has_permissions(manage_channels=True) async def logs(self, ctx, log_channel: discord.TextChannel, staff_role: discord.Role): # Check if user has manage_channels permission if not ctx.guild.me.guild_permissions.manage_channels: await ctx.send(":prayer_failed: | I am missing the `manage_channels` permission.") return

inland oak
#

since you're using @commands.has_permissions()
then you could use:

# Return a message if a user doesn't have permissions (prefix command)
@client.event
async def on_command_error(ctx, error):
   if isinstance(error, commands.MissingPermissions):
      await ctx.send("You are missing the required permissions to run this command.")

# Slash Command

@client.event
async def on_application_command_error(ctx, error):
   if isinstance(error, commands.MissingPermissions):
      await ctx.send("You are missing the required permissions to run this command.")

might vary slightly if you're using discord.py, pycord or something else. this example is based off of nextcord

little locust
#

I do not need / want the slash command.

inland oak
#

don't include it then

#

The code provided goes in your main file, being the same file that you use client.run

little locust
#

for me its not client.run its bot.run

inland oak
#

yeah, as I said it varies

#

you'll have to refer to the documentation relevant to what you're using and alter it accordingly

little locust
inland oak
#

this doesn't go in the cog

little locust
#

so it goes stragith into the main file?

inland oak
#

yes

#

the same file you run the bot, set up intents, command prefix, on_ready etc

little locust
#

quick question for you

#

is the error automatically gonna go to the logging?
like the logging problem

#

or is it gonna go for every single one

inland oak
#

What do you mean?

little locust
#

like

inland oak
# little locust

the .event will reply to the command that returned a permission error

little locust
#

I want a error, like said :prayer_failed: | I am missing the `manage_channels` permission. to be only sent to the logs and the deletelogs

inland oak
#

you would find the channel, then instead of using await ctx.send you'd use await channelName.send()

vast kettle
#

channel = bot.get_channel(CHANNEL_ID)
await channel.send("foo")

inland oak
#

the await ctx.send will send that message to the same channel that the error occurred. for example, if i tried to use a command that I don't have permissions use in #python-discussion, the bot would send your message in that channel

little locust
#

the bot will be in multiple servers, so it will all have different logs channels

inland oak
#

then you could have a database storing the channel ID of different log channels, along with the server ID

little locust
#

ido

#

including staff_role , log_channel and the server id

inland oak
#

then you'd just fetch the channel ID, find the channel in discord and use await channelName.send("hi")

little locust
#

so i put it right here?

inland oak
#

as I said, await ctx.send will send the message in the channel that is being used in

vast kettle
#

are you having a file per command?

little locust
#

yes, i want that to happend

little locust
#

yes

vast kettle
#

why

little locust
#

idk

#

more easy for me

vast kettle
#

just separate them by categories

inland oak
#

that's the idea of cogs

vast kettle
#

not even java requires so many files

little locust
#

xD

vast kettle
little locust
#

oh well

#

can we get back to fixing

#

the command

vast kettle
#

yeah

inland oak
#

cogs can be used for 1 file per command, or a file containing all commands specific to moderating or whatever you want

little locust
#

anyways

#

i do want the message to be sent in the channel that is being used in

#

but it is not sending

inland oak
#

ok

vast kettle
#

errors?

little locust
inland oak
vast kettle
#

!paste

brave vesselBOT
#
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.

little locust
#

i do not really know, im being stuck on this thing for the past hour

#

if i give u the whole code

#

can u look around

#

and see if i did something wrong

vast kettle
little locust
#

bet

inland oak
little locust
vast kettle
#

but

#

we also need the error handler

little locust
#

this is my errors

#

no errors at all

#

except the syntax dont mind that

inland oak
#

are you wanting the error to show if the user running the command has insufficient permissions, or if the bot has insufficient permissions?

little locust
#

user

inland oak
#

because ctx.guild.me refers to the bot

little locust
#

oh fuck

#

is that what i was stuck on

inland oak
#

possibly

little locust
#

this whole time

#

what do i change it to

#

my brain

#

is dead

inland oak
#

ctx.author.guild_permissions should work

little locust
#

alr

inland oak
#

if not ctx.author.guild_permissions.manage_channels:

little locust
#

alright hold on

#

let me get one of my tester to try it out

#

i just got a hella error

#
Traceback (most recent call last):
  File "d:\00 ! I prayer bot\cogs\logging.py", line 1, in <module>
    import discord
  File "C:\Users\mny\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\discord\__init__.py", line 20, in <module>
    import logging
  File "d:\00 ! I prayer bot\cogs\logging.py", line 2, in <module>
    from discord.ext import commands
  File "C:\Users\mny\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\discord\ext\commands\__init__.py", line 11, in <module>
    from .bot import *
  File "C:\Users\mny\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\discord\ext\commands\bot.py", line 28, in <module>
    import asyncio
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.752.0_x64__qbz5n2kfra8p0\Lib\asyncio\__init__.py", line 8, in <module>
    from .base_events import *
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.752.0_x64__qbz5n2kfra8p0\Lib\asyncio\base_events.py", line 18, in <module>     
    import concurrent.futures
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.752.0_x64__qbz5n2kfra8p0\Lib\concurrent\futures\__init__.py", line 8, in <module>
    from concurrent.futures._base import (FIRST_COMPLETED,
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.752.0_x64__qbz5n2kfra8p0\Lib\concurrent\futures\_base.py", line 43, in <module>    LOGGER = logging.getLogger("concurrent.futures")
             ^^^^^^^^^^^^^^^^^
AttributeError: partially initialized module 'logging' has no attribute 'getLogger' (most likely due to a circular import)```
#

nvm i just fixed it

inland oak
#

'logging' has no attribute 'getLogger' sums it up

little locust
#

mhm

#

yeah still doesnt work

inland oak
#

any errors?

little locust
#

nope

#

should i give my main bot file (dw it doesnt have the token there) and the logging.py file?

#

so u can see

#

what the problem is

inland oak
#

if you want sure

little locust
#

there ye go

inland oak
little locust
#

mhm

inland oak
#

again, in your logging file, this will be checking the permissions of the bot not the command author/user

if not ctx.guild.me.guild_permissions.manage_channels:
            await ctx.send(":prayer_failed: | You are missing the `manage_channels` permission.")
            return
little locust
#

but i do be needing that the user

inland oak
#

?

little locust
#

that is for the user

#

not for the bot

inland oak
#

that is ctx.guild.me

#

this refers to the bot, not the command author

little locust
#

oh

#

well i had it like that this whole time

#

@inland oak mind dming me and helping me in the discord server with the bot cause someone is keep going afk and i cant test

inland oak
#

alright

brave vesselBOT
#
Python help channel closed

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.