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等等。
很抱歉對此沒有明確的答案。
添加回答
舉報