我的代码很简单,因为我刚开始编写我的不和谐机器人,我想要它,所以当单词 !meme
在不和谐中发送时,机器人会从 subreddit 发送一个随机模因,这是代码:>
import discord
import os
import praw
import random
client = discord.Client()
reddit = praw.Reddit(client_id='the client id',
client_secret='the client secret',
user_agent='Memri TV Bot by /u/Hezbolloli')
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
subreddit = reddit.subreddit("memritvmemes")
all_subs = []
top = subreddit.hot(limit=50)
for submission in top:
all_subs.append(submission)
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)
client.run('Token')
我的错误就在这里,我不知道我应该从哪里开始看这个,因为这是迄今为止我在编码生涯中遇到的最长的错误:
It appears that you are using PRAW in an asynchronous environment.
It is strongly recommended to use Async PRAW: https://asyncpraw.readthedocs.io.
See https://praw.readthedocs.io/en/latest/getting_started/multiple_instances.html#discord-bots-and-asynchronous-environments for more info.
Ignoring exception in on_message
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "main.py", line 28, in on_message
for submission 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 346, in refresh
self._request_token(grant_type="client_credentials")
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
答案 0 :(得分:1)
问题似乎出在您对 Reddit 的请求上。当您在异步环境中工作时,您需要改用 asyncpraw 模块(文档:Asyncpraw for reddit docs)。代码几乎相同:
import discord
import os
import asyncpraw # install it using "pip install asyncpraw"
import random
client = discord.Client()
reddit = asyncpraw.Reddit(client_id='the client id',
client_secret='the client secret',
user_agent='Memri TV Bot by /u/Hezbolloli')
此外,您没有定义 ctx。尝试:message.channel.send(...)
,或设置一个机器人。
如果您正在编写机器人代码,我还强烈建议您使用 discord bot 命令作为添加功能的一种方式,而不是读取消息内容(您也可以使用斜杠命令)。 这是文档的链接:https://discordpy.readthedocs.io/en/stable/#getting-started 网上有很多关于这方面的信息。如果您遇到麻烦,我很乐意提供帮助。