除非不工作

时间:2018-04-22 02:35:51

标签: python discord

我有一个命令,.ud,在urbandictionary_top.的帮助下在Urban Dictionary上定义了一个单词。问题是当遇到一个没有定义的单词时,它没有&#39什么都不说

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'

第132行为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))

1 个答案:

答案 0 :(得分:1)

term = udtop(target_def)放在try

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)