Hi! So, I'm trying to make a Discord bot for my server and it's generally going pretty well except for the fact that when I try to organise my code with cogs, it doesn't seem to show up. :')
Regarding errors-- there doesn't seem to be any errors in my terminal; however, the slash command isn't showing up on my server.
Here is my code. I apologise that it's messy!
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='/')
bot.load_extension('cogs.hunt')
@bot.event
async def on_ready():
print(f"We have logged in as {bot.user}")
activity = discord.Activity(type=discord.ActivityType.watching, name="Over TTC")
await bot.change_presence(activity=activity)
bot.run('TOKEN')```
**hunt.py** (located in C:\Users\NAME\Desktop\BOT\cogs)
```py
import discord
from discord.ext import commands
import random
class Hunt(commands.Cog):
def __init__(self, bot):
self.bot = bot
# Group modifiers dictionary based on the season
group_modifiers = {
"Group1": {
# removed due to character limit
},
}
# Rolls and corresponding prey difficulties
rolls_preys = {
# removed due to character limit
}
# Group colors
group_colors = {
# removed due to character limit
}
# Helper function to apply constraints on the output number
def constrain_output(number):
return max(min(number, 15), 0)
# Function to choose between "a" and "an" based on whether the word starts with a vowel or consonant
def a_or_an(word):
vowels = ['a', 'e', 'i', 'o', 'u']
return 'an' if word.lower().startswith(tuple(vowels)) else 'a'
@commands.slash_command()
async def hunt(
ctx: discord.ApplicationContext,
group: discord.Option(str, description="The group your character is in.", choices=['Group1', 'Group2', 'Group3']),
season: discord.Option(str, description="The season you are rolling for.", choices=['Season1', 'Season2', 'Season3']),
character_modifier: discord.Option(int, description="The character modifier. Default is 0.", min_value=-4, max_value=4, default=0)
):
# Check if the modifier is within the valid range
character_modifier = max(min(character_modifier, 4), -4)
# Roll the dice with group and user modifiers
dice = random.randint(1, 15)
group_modifier = group_modifiers[group][season]
total_roll = dice + group_modifier + character_modifier
# Apply constraints on the output number
total_roll = constrain_output(total_roll)
# Gets the corresponding prey difficulty
prey_difficulty = rolls_preys[total_roll]
# Response Embed
embed = discord.Embed(title="Hunt Result", color=group_colors.get(group, discord.Color.default()))
# Add fields to the embed
# removed due to character limit
# Check if the hunt was successful or not and add the appropriate field
if prey_difficulty == "Miss":
embed.add_field(name="❗ Result:", value="You missed the prey!", inline=True)
else:
article = a_or_an(prey_difficulty)
embed.add_field(name="❗ Result:", value=f"You got {article} {prey_difficulty} prey!", inline=True)
embed.add_field(name="🐾 Hunting Document:", value="[Here](LINK)", inline=True)
# Send the embed as the response
await ctx.respond(embed=embed)
def setup(bot):
bot.add_cog(Hunt(bot))```
I'm assuming it's just something that I've overlooked. Outside of using cogs, my code works fine.
My bot does have the commands scope, py-cord is updated, legacy chat is off, and I do not have any conflicting libraries.
Thank you for taking the time to read this!