I am extremely new to Python and I want to make a Spell Generator that uses '/spell' as a command to get the Spell Generator to grab a random Harry Potter spell from this: https://hp-api.onrender.com/api/spells. I originally wanted to code this in C++/D++ since that is the language I am most comfortable with but that is proving difficult with the libraries since it's super roundabout on how to get those integrated and such, which is why I'm trying my very limited hand at Python.
The thing is I got it up and running so it's online in the server (I followed the Replit tutorial on how to make the discord bot in python) BUT when I'm trying to type in '/spell' in the textbox in a channel within the server, the command is not listed? It only brings up the general slash commands that are already in Discord and don't correlate to my command at all. There is no error when I run my code either. It's just no recognition at all of the '/spell' command that I want to exist and use.
Being very new to Python and trying to challenge myself with this project, I'm going to need some guidance on how to figure this out, please! Below is the code and I've already installed the "discord-py-slash-command" package in my Replit plus already triple-checked that I have my intents active within the Discord Dev Portal.
The status_alive is from the Replit page to let me know that the bot is live using the Web Viewer within the Replit webpage:https://docs.replit.com/tutorials/python/build-basic-discord-bot-python.
I can also provide the join link to the Replit if it'll help anyone out with clarity. I know the code is a mess so any help is appreciated! :)
import os
import discord
import requests
import json
import random
from status_alive import status_alive
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
@client.event
async def on_message(message):
if message == "/spell":
url = "https://hp-api.onrender.com/api/spells"
headers = {
"Authorization": "Bearer " + os.environ['DISCORD_BOT_SECRET'],
"Content-Type": "application/json"
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
spells = json.loads(response.text)
spell = random.choice(spells)
await message.channel.send("Spell: " + spell["spell"] + "\nType: " + spell["type"])
else:
await message.channel.send("Error: " + response.text)
status_alive()
my_secret = os.environ['DISCORD_BOT_SECRET']
client.run(my_secret)
Here is the error I am running into and for testing purposes, I already made the bot have Admin permissions within the Dev Portal, and made it an actual Admin within the server I added the bot to. "Bot" and "application.commands" is enabled within the scopes; do I need to be adding more scopes? There are a lot that just ask for a "Redirect URL" when I enable it which I don't need.
Traceback (most recent call last):
File "/home/runner/hp-py/venv/lib/python3.10/site-packages/discord/client.py", line 409, in _run_event
await coro(*args, **kwargs)
File "main.py", line 17, in on_ready
await tree.sync(guild=discord.Object(id=MY ID IS HERE))
File "/home/runner/hp-py/venv/lib/python3.10/site-packages/discord/app_commands/tree.py", line 1071, in sync
data = await self._http.bulk_upsert_guild_commands(self.client.application_id, guild.id, payload=payload)
File "/home/runner/hp-py/venv/lib/python3.10/site-packages/discord/http.py", line 738, in request
raise Forbidden(response, data)
discord.errors.Forbidden: 403 Forbidden (error code: 50001): Missing Access