Python ';上下文';对象没有属性';reddit';不和谐
我正在尝试向我的bot添加Python ';上下文';对象没有属性';reddit';不和谐,python,discord.py,Python,Discord.py,我正在尝试向我的bot添加.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
.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)
我已经导入了所有内容,在我出现以下错误之前,所有内容都看起来很好:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Command' object has no attribute 'randint'
据我所知,程序不知道randint是什么。我检查了一下我是否打错了,但没有。一切似乎都很好。我在同一个命令上遇到了另一个错误,但我设法解决了它。但这一次抓住了我,我需要你的帮助
以下是新的错误:
AttributeError: 'coroutine' object has no attribute 'url'
你有Cog(类)中的命令吗 如果没有,那么应该删除
self
,因为它将假定这是上下文
对象的名称
@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
冲突。您可以在decorator中使用name=
关键字参数设置命令的名称。人们将在discord中键入该名称
尝试使用您的随机提交方法(减去多余的代码,逻辑相同),它对我有效:
reddit=praw.reddit(client_id=“…”,client_secret=“…”,user_agent=“…”)
@bot.command(name=“reddit”)
异步def_reddit(ctx,subreddit:str=”“):
submissions=reddit.subreddit(subreddit.hot)()
提交=下一个(如果不是x,则提交中的x代表x)
等待ctx.send(submission.url)
我能想到的唯一一件事是确保您拥有最新版本的praw
,而且如果命令中有任何其他内容您可能会遗漏,那么这可能会影响它,尽管这只是猜测
我想说的是,试着从头开始创建命令。从简单的工作开始,一行一行地添加到命令中,直到有东西中断。然后你就会知道是什么导致了运行时警告等等
很抱歉没有一个明确的答案。评论不适用于长时间的讨论;此对话已结束。
@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")