Jacob Cook
Jacob Cook

Reputation: 1

How do I make it so my discord bot on request sends a meme from reddit?

My code is simple because I am new to coding my discord bot, I want it so the bot sends a random meme from a subreddit when the word !meme is sent in discord, here is the code:

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')

My error is here and I am not sure where I should start looking at this because this is by far the longest error I have ever got in my coding career:

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

Upvotes: 0

Views: 771

Answers (1)

Olivrv
Olivrv

Reputation: 11

It seems the problem lies in your request to Reddit. As you are working in a asynchronous environment, you need to use the asyncpraw module instead (docs: Asyncpraw for reddit docs). The code is almost the same:

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')

Also, you did not define ctx. Try : message.channel.send(...), or setup a bot.

If you are coding a bot, I would also strongly recommend that you use the discord bot commands as a way of adding functions, instead of reading the messages content (you could also use slash commands). Here is a link to the docs : https://discordpy.readthedocs.io/en/stable/#getting-started There is quite a lot of info about that online. If you run into trouble I'd be glad to help.

Upvotes: 1

Related Questions