Home > database >  Discord bot stopped working through Heroku, works fine offline
Discord bot stopped working through Heroku, works fine offline

Time:04-23

Solved! Answer below.

EDIT: Cleaned up this post to keep it all clear.

Here's the code for a simple GIF bot. The important thing is: this bot works offline, gives a gif when I send a !command. This bot comes online with Heroku, so that is also good. What doesn't work is sending commands to receive gifs when the bot is online. So the code works, but I'm having trouble getting it in a working state with the commands in online mode.

Code

import random
import discord
import giphy_client
from discord.ext.commands import Bot
from giphy_client.rest import ApiException

discord_token = *hidden for stackoverflow*

giphy_token = *hidden for stackoverflow*

api_instance = giphy_client.DefaultApi()

def search_gifs(query):
    try:
        return api_instance.gifs_search_get(giphy_token, query, limit=100, rating = 'PG13')

    except ApiException as e:
        return "Exception when calling DefaultApi->gifs_search_get: %s\n" % e

def gif_response(emotion):
    gifs = search_gifs(emotion)
    lst = list(gifs.data)
    gif = random.choices(lst)

    return gif[0].url

class DiscordClient(discord.Client):
    async def on_ready(self):
        print("Login as")
        print(self.user)
        print("-------")

    async def on_message(self, message):

        if message.author != self.user:
            if message.content == '!vibe':
                await message.channel.send(gif_response('vibing'))
            if message.content == '!dragony':
                await message.channel.send(gif_response('dragon'))  
            if message.content == '!gm':
                await message.channel.send(gif_response('gm'))    

intents = discord.Intents(messages=True, guilds=True, guild_messages=True)
client = DiscordClient(intents=intents)
# client = DiscordClient()
client.run(discord_token)

Requirements file (requirements.txt)

git https://github.com/Rapptz/discord.py
PyNaCl==1.3.0
dnspython==1.16.0
async-timeout==3.0.1
pandas
giphy_client==1.0.0

Procfile

worker: python gif_bot.py

Logs while deploying

2022-04-21T11:46:39.000000 00:00 app[api]: Build started by user 
2022-04-21T11:47:14.667290 00:00 app[api]: Deploy x by user 
2022-04-21T11:47:14.667290 00:00 app[api]: Release v11 created by user 
2022-04-21T11:47:15.800842 00:00 heroku[worker.1]: State changed from crashed to starting
2022-04-21T11:47:21.019690 00:00 heroku[worker.1]: Starting process with command `python gif_bot.py`
2022-04-21T11:47:21.640978 00:00 heroku[worker.1]: State changed from starting to up
2022-04-21T11:47:25.392719 00:00 app[worker.1]: Login as
2022-04-21T11:47:25.392731 00:00 app[worker.1]: Gif Bot#8552
2022-04-21T11:47:25.392732 00:00 app[worker.1]: -------
2022-04-21T11:47:29.000000 00:00 app[api]: Build succeeded

CodePudding user response:

@smerkd has helped me with the answer!

Adding "message_content = True" to the intents made it work.

Code

intents = discord.Intents(messages=True, message_content=True, guilds=True, guild_messages=True)

CodePudding user response:

With newer versions of discord you need to pass your intents. Essentially telling discord what events you're going to be using and some cache stuff. A simple way to think of it is like what you're intending to do or permissions.

For your example try the following

intents = discord.Intents.default()
intents.message_content = True

Then fix your client to include the specified intents

client = DiscordClient(intents=intents)

Read up more about them here: https://discordpy.readthedocs.io/en/stable/intents.html

Note: you will have to add more intents if you're planning on doing anything more complicated.

  • Related