我该如何让我的discord机器人在请求时从reddit发送一个表情包



我的代码很简单,因为我刚开始编码我的discord bot,我想要它,这样当单词!meme不一致发送时,bot就会从子reddit发送一个随机模因,下面是代码:

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

问题似乎出在你对Reddit的请求上。当您在异步环境中工作时,需要使用异步模块(docs: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-已启动网上有很多关于这方面的信息。如果你遇到麻烦,我很乐意帮忙。

最新更新