Search code examples
pythondiscorddiscord.py

Slash Commands Not showing up in Discord Server


I wrote Slash commands for a discord bot in python. However even after waiting over 2 hours, the slash commands are not showing up in guilds not listed under guild_ids. I made sure to include application commands in the invite link

Heres all my code

from discord.ext import commands
import os
from dislash import slash_commands
from dislash.interactions import *
import tbapy
from keep_alive import keep_alive
from events import event_names, event_ids
import difflib
from datetime import date
import json
from test_guilds import test_guilds


tba = tbapy.TBA(os.environ['tbakey'])
client = commands.Bot(command_prefix="tba.", case_sensetive=False)
client.remove_command('help')
token = os.environ['TOKEN']
slash = slash_commands.SlashClient(client)
todays_date = date.today()


@client.event
async def on_ready():
  await client.change_presence(activity=discord.Game(name="/learn or tba.learn for help"))
  print("ready!")

@client.command()
async def learn(ctx):
  embed = discord.Embed(title="Help")
  embed.add_field(name="how to use Blue Alliance", value="to use the bot, start by typing '/' which should make a box appear in which you have a list of slash command available for the bot. To learn how to use them, read the desrciptions of the command.")
  await ctx.send(embed = embed)

  await ctx.send("try running '/media number: 341' to try it out!")


@slash.command(
  name="learn",
  description="help command",
  guild_ids=test_guilds,
)
async def learn(ctx):
  embed = discord.Embed(title="Help")
  embed.add_field(name="how to use Blue Alliance", value="to use the bot, start by typing '/' which should make a box appear in which you have a list of slash command available for the bot. To learn how to use them, read the desrciptions of the command.")
  await ctx.send(embed = embed)

  await ctx.send("try running '/media number: 341' to try it out!")





@slash.command(
  name="years",
  description="years a team has been competing",
  guild_ids=test_guilds,
  options=[
    Option(
      name="number",
      description="team number",
      type=Type.INTEGER,
      required=True
      
    )
  ]
  
)
async def years(ctx, number):
  year_list = tba.team_years(number)
  nick = tba.team(number, simple=True)
  nick1 = nick['nickname']
  embed = discord.Embed(title=f"Years of {nick1} - {number}")
  embed.add_field(name="Rookie Year", value=f"{year_list[0]}")
  embed.add_field(name="Most Recent Year", value=f"{year_list[len(year_list) - 1]}")

  for i in range(len(year_list) - 2):
    if (year_list[i + 1] - year_list[i]) == 1:
      skipped=False
    else:
      embed.add_field(name="Skipped Year", value=f"{(year_list[i]) + 1}", inline=True)

  if not skipped:
    embed.add_field(name="Skipped Years", value="No Skipped Years", inline=False)
  await ctx.send(embed=embed)
  

  
@slash.command(
  name="media",
  description="get media on team",
  guild_ids=test_guilds,
  options=[
    Option(
      name="number",
      description="team number",
      required=True,
      type=Type.INTEGER
    ),
    Option(
      name="year",
      description="year of media",
      required=False,
      type=Type.INTEGER
    )
  ]
)
async def media(ctx, number, year=todays_date.year):
  media = tba.team_media(number, year)
  nick = tba.team(number, simple=True)
  nick1 = nick['nickname']
  if not media:
    await ctx.send(f"No media for {nick1} - {number} in {year}")
    await ctx.send(f"Add Some at https://www.thebluealliance.com/team/{number}/{year}")
  else:
    media1 = media[0]
    await ctx.send(media1['view_url'])
    
@slash.command(
  name="event_info",
  description="get info about a team at an event",
  guild_ids=test_guilds,
  options=[
    Option(
      name="team",
      description="team number",
      required=True,
      type=Type.INTEGER
    ),
    Option(
      name="event",
      description="event name",
      required=True,
      type=Type.STRING
    ),
    Option(
      name="year",
      description="the year of the event",
      required=False,
      type=Type.INTEGER
    )
  ]
)
async def event_info(ctx, team, event, year=todays_date.year):
  for i in range(len(event_names)):
    if event in event_names[i]:
      match = i
    else:
      pass

  stats = tba.team_status(team, f"{year}{event_ids[match]}")
  nick = tba.team(team, simple=True)
  nick1 = nick['nickname']
  #print(stats)
  alliance = stats['alliance']['name']
  pick = stats['alliance']['pick']
  overall = stats['overall_status_str']
  overall = overall.replace("<b>", "``")
  overall = overall.replace("</b>", "``")

  
  embed = discord.Embed(title=f"Stats for ```{nick1} ``` at ```{event_names[match]}``` ({year})")
 # embed.add_field(name=f"Alliance Pick", value=f"Pick {pick} for {alliance}")
  embed.add_field(name="Overall", value=overall)
  await ctx.send(embed=embed)

keep_alive()
client.run(token) ````

Solution

  • The guild_ids parameter only can accept 1 value in the list. Therefore, test_guilds can store 1 server ID only.

    If you want to register your slash command globally, remove guild_ids parameter and wait 12 hours to let discord register your slash command globally.