首页 > 解决方案 > Discord.py meme bot with praw

问题描述

我正在尝试使用 discord.py 制作一个不和谐的机器人。我添加的命令之一是用于扩大 meme 经济的 meme 命令。我会使用 praw 将模因从 reddit 发布到服务器。我按照在线教程进行操作,但是当我尝试运行它时出现此错误

Ignoring exception in command meme:
        Traceback (most recent call last):
          File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
            ret = await coro(*args, **kwargs)
          File "main.py", line 34, in meme
            for lowfsdf in top:
          File "/opt/virtualenvs/python3/lib/python3.8/site-packages/praw/models/listing/generator.py", line 63, in __next__
            self._next_batch()
          File "/opt/virtualenvs/python3/lib/python3.8/site-packages/praw/models/listing/generator.py", line 73, in _next_batch
            self._listing = self._reddit.get(self.url, params=self.params)
          File "/opt/virtualenvs/python3/lib/python3.8/site-packages/praw/reddit.py", line 566, in get
            return self._objectify_request(method="GET", params=params, path=path)
          File "/opt/virtualenvs/python3/lib/python3.8/site-packages/praw/reddit.py", line 666, in _objectify_request
            self.request(
          File "/opt/virtualenvs/python3/lib/python3.8/site-packages/praw/reddit.py", line 848, in request
            return self._core.request(
          File "/opt/virtualenvs/python3/lib/python3.8/site-packages/prawcore/sessions.py", line 324, in request
            return self._request_with_retries(
          File "/opt/virtualenvs/python3/lib/python3.8/site-packages/prawcore/sessions.py", line 222, in _request_with_retries
            response, saved_exception = self._make_request(
          File "/opt/virtualenvs/python3/lib/python3.8/site-packages/prawcore/sessions.py", line 179, in _make_request
            response = self._rate_limiter.call(
          File "/opt/virtualenvs/python3/lib/python3.8/site-packages/prawcore/rate_limit.py", line 33, in call
            kwargs["headers"] = set_header_callback()
          File "/opt/virtualenvs/python3/lib/python3.8/site-packages/prawcore/sessions.py", line 277, in _set_header_callback
            self._authorizer.refresh()
          File "/opt/virtualenvs/python3/lib/python3.8/site-packages/prawcore/auth.py", line 373, in refresh
            self._request_token(
          File "/opt/virtualenvs/python3/lib/python3.8/site-packages/prawcore/auth.py", line 155, in _request_token
            response = self._authenticator._post(url, **data)
          File "/opt/virtualenvs/python3/lib/python3.8/site-packages/prawcore/auth.py", line 38, in _post
            raise ResponseException(response)
        prawcore.exceptions.ResponseException: received 401 HTTP response
        The above exception was the direct cause of the following exception:
        Traceback (most recent call last):
          File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 902, in invoke
            await ctx.command.invoke(ctx)
          File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 864, in invoke
            await injected(*ctx.args, **ctx.kwargs)
          File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
            raise CommandInvokeError(exc) from exc
        discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ResponseException: received 401 HTTP response

我对 discord.py 比较陌生,但这是我的代码。另外,如果重要的话,我正在使用https://repl.it来托管我的不和谐机器人。

from discord.ext import commands
import praw
TOKEN = #token
intents = discord.Intents(messages = True, members = True)
bot = commands.Bot(command_prefix = commands.when_mentioned_or('$'), intents = intents) 
reddit = praw.Reddit(client_id = #client id'
client_secret = #client_secret, 
username = # username, 
password = #password,  
user_agent = 'pythonpraw',check_for_async = False)
@bot.command()
async def meme(ctx):
    subreddit = reddit.subreddit("memes")
    all_subs = []
    top = subreddit.hot(limit = 50)
    for lowfsdf in top:
        all_subs.append(lowfsdf)
    random_sub = random.choice(all_subs)
    name = random_sub.title 
    url = random_sub.url
    em = discord.Embed (title = name)
    em.set_image(url = url)
    await ctx.send(embed = em) 
bot.run(TOKEN)

为什么它会返回此错误?我能做些什么来解决它?

标签: pythondiscord.pypraw

解决方案


推荐阅读