Search code examples
discord.pyofflineredditpraw

Discord.py Bot is not online, but still works


i have a problem with my discord bot, whenever i run the code below using apraw to get the titles of the recent submissions on a subreddit the bot doesn't appear online anymore but still returns the titles in CMD :

  1. Bot is not online when i execute this but still asks for subreddit name & prints the titles of the new posts of the subreddit in CMD:
import asyncio
import apraw
from discord.ext import commands

bot = commands.Bot(command_prefix = '?')

@bot.event
async def on_ready():
    print('Bot is ready')
    await bot.change_presence(status=discord.Status.online, activity=discord.Game('?'))

@bot.command()
async def online (ctx):
    await ctx.send('Bot is online !')

reddit = apraw.Reddit(client_id = "CLIENT_ID",
                      client_secret = "CLIENT_SECRET",
                      password = "PASSWORD",
                      user_agent = "pythonpraw",
                      username = "LittleBigOwl")

@bot.event
async def scan_posts():
    xsub = str(input('Enter subreddit name : '))
    subreddit = await reddit.subreddit(xsub)
    async for submission in subreddit.new.stream():
        print(submission.title)

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(scan_posts())


bot.run('TOKEN')
  1. But is online when i execute this but obviously doesn't ask for sub name... :
import asyncio
import apraw
from discord.ext import commands

bot = commands.Bot(command_prefix = '?')

@bot.event
async def on_ready():
    print('Bot is ready')
    await bot.change_presence(status=discord.Status.online, activity=discord.Game('?'))

@bot.command()
async def online (ctx):
    await ctx.send('Bot is online !')


bot.run('TOKEN')

So reddit is the problem here. But what exaclty do i ahve to change in order to make my bot apear online whilst still being able to retreive the titles of new submissions on a given subreddit? The code doesn't return any error:/


Solution

  • the @bot.event that you put above async def scan_posts(): should be changed to @bot.command(). It triggers by itself because you have this in your code:

    if __name__ == "__main__":
        loop = asyncio.get_event_loop()
        loop.run_until_complete(scan_posts())
    

    this is what causes the scan_posts to run automatically. You should remove this as you want scan_posts to be a bot command and not something that happens automatically. The bot also doesn't come online because of this code. What this does is check if this file has been run and then runs scan_posts. The rest of the code won't be triggered. Also, you shouldn't change presence in on_ready as Discord has a high chance to completely disconnect you during the on_ready event and there is nothing you can do to prevent it. Instead you can use the activity and status kwargs in commands.Bot like:

    bot = commands.Bot(command_prefix = '?', status=discord.Status.online, activity=discord.Game('?'))