UrbanDict.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:CorpBot.py 作者: corpnewt 项目源码 文件源码
def define(self, ctx, *, word : str):
        """Gives the definition of the word passed."""

        # Check if we're suppressing @here and @everyone mentions
        if self.settings.getServerStat(ctx.message.server, "SuppressMentions").lower() == "yes":
            suppress = True
        else:
            suppress = False

        if not word:
            msg = 'Usage: `{}define [word]`'.format(ctx.prefix)
            await self.bot.send_message(ctx.message.channel, msg)
            return
        url = "http://api.urbandictionary.com/v0/define?term={}".format(quote(word))
        msg = 'I couldn\'t find a definition for "{}"...'.format(word) 
        r = requests.get(url, headers = {'User-agent': self.ua})
        theJSON = r.json()["list"]
        if len(theJSON):
            # Got it - let's build our response
            ourWord = theJSON[0]
            msg = '__**{}:**__\n\n{}'.format(string.capwords(ourWord["word"]), ourWord["definition"])
            if ourWord["example"]:
                msg = '{}\n\n__Example(s):__\n\n*{}*'.format(msg, ourWord["example"])

        # await self.bot.send_message(ctx.message.channel, msg)
        # Check for suppress
        if suppress:
            msg = Nullify.clean(msg)
        await Message.say(self.bot, msg, ctx.message.channel, ctx.message.author)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号