Except isn't Working

Mehvix

I have a commands, .ud, that defines a word on Urban Dictionary with the help of urbandictionary_top. The issue is that when it comes across a word that doesn't have a definition it doesn't do anything say

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\MaxPC\PycharmProjects\synapsBot 2.0\venv\lib\site-packages\discord\client.py", line 307, in _run_event
    yield from getattr(self, event)(*args, **kwargs)
  File "C:/Users/MaxPC/PycharmProjects/synapsBot 2.0/synapsBot 2.04.py", line 132, in on_message
    term = udtop(target_def)
  File "C:\Users\MaxPC\PycharmProjects\synapsBot 2.0\venv\lib\site-packages\urbandictionary_top.py", line 28, in __init__
    top = soup.find(class_="meaning").text
AttributeError: 'NoneType' object has no attribute 'text'

with line 132 being term = udtop(target_def)

import discord
from urbandictionary_top import udtop
from discord.ext.commands import Bot
from discord.ext import commands
@client.event
async def on_message(message):
    user_id = message.author.id
    # UD Code
    elif message.content.upper().startswith('.UD'):
        target_def = message.content[4:]
        term = udtop(target_def)

        try:
            embed = discord.Embed(title='Definition Page', url='https://www.urbandictionary.com/define.php?term={}'
                                  .format(target_def), color=0x0080c0)
            embed.set_author(name='Definition for ' + target_def, url='https://www.urbandictionary.com/define.php?'
                                                                      'term={}'.format(target_def))
            embed.set_footer(text=term)
            await client.send_message(message.channel, embed=embed)
        except AttributeError:
            await client.send_message(message.channel, 'Sorry, `{0}` has no definition! You can add your own definition'
                                                       ' at https://www.urbandictionary.com/add.php?word={1}'
                                      .format(target_def, target_def))
Morse

Put term = udtop(target_def) within try block

elif message.content.upper().startswith('.UD'):
    target_def = message.content[4:]

    try:
        term = udtop(target_def)
        embed = discord.Embed(title='Definition Page', url='https://www.urbandictionary.com/define.php?term={}'
                              .format(target_def), color=0x0080c0)
        embed.set_author(name='Definition for ' + target_def, url='https://www.urbandictionary.com/define.php?'
                                                                  'term={}'.format(target_def))
        embed.set_footer(text=term)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related