第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

“上下文”對象沒有屬性“reddit”Discord.PY

“上下文”對象沒有屬性“reddit”Discord.PY

嗶嗶one 2022-12-20 15:27:57
我正在嘗試向.reddit我的機器人添加命令。這是我的代碼: @client.command(name="random", aliases=["reddit"]) async def _random(ctx, subreddit: str = ""):    reddit = None    if reddit_app_id and reddit_app_secret:        reddit = praw.Reddit(client_id=reddit_app_id,client_secret=reddit_app_secret,user_agent="MASTERBOT:%s1.0" % reddit_app_id)    if reddit:        submissions = reddit.subreddit(subreddit).hot()        submission = next(x for x in submissions if not x.stickied)        await ctx.send(submissions.url)我已經導入了所有內容,一切似乎都很好,直到出現(xiàn)此錯誤:discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Command' object has no attribute 'randint'據(jù)我了解,該程序不知道什么是 randint。我檢查了我是否打錯了字,但沒有。一切似乎都很好。我在同一命令上遇到了錯誤,但我設法修復了它。但這一個抓住了我,我需要你的幫助。這些是新錯誤:AttributeError: 'coroutine' object has no attribute 'url'.RuntimeWarning: Enable tracemalloc to get the object allocation traceback
查看完整描述

1 回答

?
嚕嚕噠

TA貢獻1784條經驗 獲得超7個贊

你有 Cog(類)中的命令嗎?


如果不這樣做,那么您應該刪除self,因為它會假定那是context對象的名稱。


@client.command(name="random", aliases=["reddit"])

async def _random( ctx, subreddit: str = ""):

    reddit = None

    if reddit_app_id and reddit_app_secret:

        reddit = praw.Reddit(client_id=reddit_app_id,client_secret=reddit_app_secret,user_agent="MASTERBOT:%s1.0" % reddit_app_id)

    if reddit:

        chosen_subreddit = reddit_enabled_meme_subreddits[0]

        if subreddit:

            if subreddit in reddit_enabled_meme_subreddits:

                chosen_subreddit = subreddit

        submissions = reddit.subreddit(chosen_subreddit).hot()

        post_to_pick = random.randint(1, 10)

        for i in range(0, post_to_pick):

            submission = next(x for x in submissions if not x.stickied)

        await ctx.send(submission.url)

    else:

        await ctx.send("This is not working")

問題出在命令的名稱上random,因為這會污染random模塊的命名空間。您可以通過重命名命令來繞過它。


與代碼頂部的沖突async def random(....。import random您可以name=在裝飾器中使用關鍵字參數(shù)設置命令的名稱。這就是人們將輸入不和諧的名字。


嘗試使用您獲得隨機提交的方法(減去多余的代碼,只是相同的邏輯),它對我有用:


reddit = praw.Reddit(client_id="...", client_secret="...", user_agent="...")


@bot.command(name="reddit")

async def _reddit(ctx, subreddit: str = ""):

    submissions = reddit.subreddit(subreddit).hot()

    submission = next(x for x in submissions if not x.stickied)

    await ctx.send(submission.url)

我唯一能想到的就是確保您擁有最新版本的praw,并且如果命令中還有其他您可能遺漏的問題,那么這可能會影響它,盡管那只是猜測。


我會說嘗試從頭開始發(fā)出命令。從簡單的東西開始,逐行添加,直到出現(xiàn)問題。然后你就會知道是什么原因造成的RuntimeWarning等等。


很抱歉對此沒有明確的答案。


查看完整回答
反對 回復 2022-12-20
  • 1 回答
  • 0 關注
  • 140 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號