0
try:
    channel1 = self.client.get_channel(self.grab_log_id(channel))
    await channel.guild.kick(i.user, reason="Assistant Anti: Deleting Channels")
    lel = discord.Embed(title='Action Assisted', description=f'**Target** : {i.user} | `{i.user.id}` \n **Action** : Kicked; Deleted Channel')
    await channel1.send(embed=lel)
    return

except Forbidden:
    emd=discord.Embed(title="⚠️ Warning ⚠️", description="I am missing the necessary permissions to perform actions. \n Check if: \n   - I am above __ALL__ users. \n   - I have __Administrator__ permission.")
    await channel1.send(f"{channel.guild.owner.mention}")
    await channel1.send(embed=emd)

except AttributeError:
    print("F")

error:

AttributeError: 'NoneType' object has no attribute 'send'

i know what the problem is, and to solve it need to add code to except attribute error, however i am still getting the error, where do i input it?, i am having trouble in actually handling it, once it can print F, i am able to add the channel id into a json and send the log

Peter Wood
  • 23,859
  • 5
  • 60
  • 99
  • Post the full traceback please. The big red bunch of jumbo your IDE outputs whenever the `AttributeError` is raised. – Captain Trojan Jul 28 '21 at 22:35
  • 1
    If you know `channel1` is `None` you don't have to do exception handling. – Peter Wood Jul 28 '21 at 22:35
  • @PeterWood Wait, if you raise an exception within an `except` block, the consecutive `except` block can catch it? You don't need to nest another `try-except` in? – Captain Trojan Jul 28 '21 at 22:37
  • No, I'm saying [Look Before You Leap](https://docs.python.org/3/glossary.html#term-lbyl), rather than [Ask Forgiveness Rather Than Permission](https://docs.python.org/3/glossary.html#term-eafp), in this case. See https://stackoverflow.com/questions/11360858/what-is-the-eafp-principle-in-python – Peter Wood Jul 28 '21 at 22:42
  • @CaptainTrojan No. Your `except` blocks will only catch exceptions raised in the `try` block. You need a nested `try` inside the `except Forbidden:` to catch exceptions there. – Barmar Jul 28 '21 at 22:43
  • The traceback should say that this error happened while handling the `Forbidden` exception. – Barmar Jul 28 '21 at 22:44
  • 1
    Just add something like `if channel1 is None: ...` after defining `channel1` and handle it however you want. – Break Jul 28 '21 at 22:52

1 Answers1

0

Your except block shouldn't assume that anything in the try block succeeded. In this case, you create channel_1 in your try, so its except shouldn't try to use it.

I'd structure it like this:

try:
    channel1 = self.client.get_channel(self.grab_log_id(channel))
    assert channel1 is not None  # not sure if this is needed with this API?
except:
    # failed to get channel1 at all!
    print("F")
    return

try:
    await channel.guild.kick(i.user, reason="Assistant Anti: Deleting Channels")
    lel = discord.Embed(title='Action Assisted', description=f'**Target** : {i.user} | `{i.user.id}` \n **Action** : Kicked; Deleted Channel')
    await channel1.send(embed=lel)
except Forbidden:
    emd=discord.Embed(title="⚠️ Warning ⚠️", description="I am missing the necessary permissions to perform actions. \n Check if: \n   - I am above __ALL__ users. \n   - I have __Administrator__ permission.")
    await channel1.send(f"{channel.guild.owner.mention}")
    await channel1.send(embed=emd)

You can also nest try/except blocks but this is difficult to read/follow IMO:

try:
    channel1 = self.client.get_channel(self.grab_log_id(channel))
    await channel.guild.kick(i.user, reason="Assistant Anti: Deleting Channels")
    lel = discord.Embed(title='Action Assisted', description=f'**Target** : {i.user} | `{i.user.id}` \n **Action** : Kicked; Deleted Channel')
    await channel1.send(embed=lel)
    return

except Forbidden:
    try:
        emd=discord.Embed(title="⚠️ Warning ⚠️", description="I am missing the necessary permissions to perform actions. \n Check if: \n   - I am above __ALL__ users. \n   - I have __Administrator__ permission.")
        await channel1.send(f"{channel.guild.owner.mention}")
        await channel1.send(embed=emd)
    except AttributeError:
        print("F")
Samwise
  • 68,105
  • 3
  • 30
  • 44