0

I recently made a discord bot for testing and the bot was supposed to send a inspirational quote from a online database on a command. However, when I entered the command it doesnt seem to work.

I tried to troubleshoot the code, but I couldn't find what was wrong. The bot sends a message to my server and specific defined channel whenever I press run but that seems to be all it does. Example of what it does

This is my code:

import discord    
from discord.ext import commands, tasks
import requests

intents = discord.Intents(messages=True, guilds=True)
bot = commands.Bot(command_prefix="?", intents=intents)


@bot.event
async def on_ready():
  print("Bot is ready.")
  quote.start()


@tasks.loop(hours=24)
async def quote():
  response = requests.get("https://zenquotes.io/api/random")
  json_data = response.json()
  quote = json_data[0]["q"] + " -" + json_data[0]["a"]
  channel = bot.get_channel(channel-id)  # use your channel id here
  await channel.send(quote)


@quote.before_loop
async def before_quote():
  await bot.wait_until_ready()
  print("Finished waiting")


@bot.command()
async def qod(ctx):
  response = requests.get("https://zenquotes.io/api/random")
  json_data = response.json()
  quote = json_data[0]["q"] + " -" + json_data[0]["a"]
  await ctx.send(quote)


bot.run(
    "bot-token")

I am using repl.it to run my program and I am researching ways to keep it running nonstop.

If anything else is needed please let me know. Thank you for reading this.

Sorry if I made any mistakes, its my first time using stack.

1 Answers1

0

You haven't actually enabled the message intent: use discord.Intents.all() to do so. Note that the way you are using commands (through messages) is not recommended anymore (see this post by discord - you should instead use application commands. Here is the updated code to use application commands:

import discord
from discord import app_commands
from discord.ext import tasks


intents = discord.Intents(discord.Intents.guilds.flag) # We don't need the message content intent
bot = discord.Client(intents=intents)
tree = app_commands.CommandTree(bot)


@bot.event
async def on_ready():
  # replace GUILD_ID with your server's ID. Remove this argument to make it work on all servers. Note that doing this will mean that it can take some time to register.
  await tree.sync(guild=discord.Object(GUILD_ID))
  print("Bot is ready.")
  quote.start()


@tasks.loop(hours=24)
async def quote():
    response = requests.get("https://zenquotes.io/api/random")
    json_data = response.json()
    quote = json_data[0]["q"] + " -" + json_data[0]["a"]
    channel = bot.get_channel(CHANNEL_ID)  # use your channel id here
    await channel.send(quote)


@quote.before_loop
async def before_quote():
  await bot.wait_until_ready()
  print("Finished waiting")

# replace GUILD_ID with your server's ID. Remove this argument to make it work on all servers. Note that doing this will mean that it can take some time to register.
@tree.command(name="qod", description="Quote", guild=discord.Object(GUILD_ID))
async def qod(interaction):
    response = requests.get("https://zenquotes.io/api/random")
    json_data = response.json()
    quote = json_data[0]["q"] + " -" + json_data[0]["a"]
    await interaction.response.send_message(quote)


bot.run("bot-token")

See this stackoverflow thread for more information.

Jaffa
  • 189
  • 1
  • 6