0

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
Sahar
  • 13
  • 5
  • Does this answer your question? [How do i make a working slash command in discord.py](https://stackoverflow.com/questions/71165431/how-do-i-make-a-working-slash-command-in-discord-py) – CrazyChucky Feb 03 '23 at 01:42
  • @CrazyChucky That is super helpful thank you! I was looking at SO articles for a while but I must have searched it up wrong. I'll reply after trying it out – Sahar Feb 03 '23 at 16:15
  • @CrazyChucky looking at the link and trying it on my own, i notice that this bot is just for a regular server, theres nothing going on with guilds? Unless guils are the same thing? Because I know theres a guild sync permission but i don't feel like I need to use that. I have the tree command written in my code but I dont know how to modify it for a server instead of a guild – Sahar Feb 03 '23 at 19:52
  • A guild and a server are the same thing. It's a confusing choice of terms. (Honestly "guild" is a lot less misleading, since they in no way correspond to physical servers.) – CrazyChucky Feb 03 '23 at 22:44
  • @CrazyChucky oh okay I tried it with the guild code in that article with no revision besides adding my ID and it wasn't working. I tried multiple solutions in the article but I am getting the error code that I am missing access. I know that I gave my bot all the permissions that it needed and even tried admin one but it didn't work. Do I need to be including a scope like guilds? I know that asks for a redirect URL so I haven't been too sure on trying – Sahar Feb 03 '23 at 23:14
  • @CrazyChucky i added the error code I am getting – Sahar Feb 06 '23 at 17:29

1 Answers1

-1

i think on line 8 you calling intents agian is setting them back to original settings and not yours, although this is only a guess since i have never had to use intents in the script itself. edit: noticed you are also trying to call a command in the @client.event and not @client.command, you need one for every command you want to run

yes
  • 1
  • 1