@bot.modal not getting executed

36 Views Asked by At
import interactions
import json
import requests
from TOKEN import secret

bot = interactions.Client(token=secret)

@bot.event
async def on_ready():
    print("running...")
    
@bot.command(
    name="group",
    description="anything group related",
    scope=1140711810174033922,
    options=[
        interactions.Option(
            name="create",
            description="create a clan with your friends",
            type=interactions.OptionType.SUB_COMMAND,
        ),
        interactions.Option(
            name="invite",
            description="invite your friends to your group",
            type=interactions.OptionType.SUB_COMMAND,
            options=[
                interactions.Option(
                    name="selection",
                    description="select your friend",
                    type=interactions.OptionType.USER,
                    required=True,
                )
            ]
        )
    ]
)
async def group_forming(ctx: interactions.CommandContext, sub_command: str):
    if sub_command == "create":
        modal = interactions.Modal(
            title="Creating a group",
            custom_id="settings",
            components=[interactions.TextInput(
            style=interactions.TextStyleType.SHORT,
            label="input the name of your group here",
            custom_id="name1",
            required=True,
            placeholder="Funny Apes...",
            min_length=3,
            max_length=16,
            ),
            interactions.TextInput(
            style=interactions.TextStyleType.PARAGRAPH,
            label="What do you want to achieve as a group",
            custom_id="goals",
            required=True,
            placeholder="We strive to get as many bananas as possible...",
            min_length=7,
            ),
            interactions.TextInput(
            style=interactions.TextStyleType.SHORT,
            label="input your favourite color here as hex code",
            required=True,
            max_length=6,
            min_length=6,
            placeholder="ffff00...",
            custom_id="color",
            )]
        )
        await ctx.popup(modal)
    if sub_command == "invite":
        with open("data.json") as file:
            data = json.load(file)
        try:
            if data["servers"][ctx.guild_id]["users"][str(ctx.member.id)]["group"] == None:
                await ctx.author.send("You aren't in a group on this server")
                
        except:
            await ctx.author.send("You aren't in a group on this server")
        accept_button = interactions.Button(
            style=interactions.ButtonStyle.SUCCESS,
            label="ACCEPT",
            custom_id="accept",
        )
        deny_button = interactions.Button(
            style=interactions.ButtonStyle.DANGER,
            label="DENY",
            custom_id="deny",
        )
        ctx.target = ctx.user
        await ctx.target.send(str(ctx.member) + ' send you an invite to the group ' + str(
            interactions.get(
            type=interactions.role, 
            object_id=data["servers"][ctx.guild_id]["users"][str(ctx.member.id)]["group"])),
            components=interactions.ActionRow(components=[accept_button, deny_button]))
        return ctx.target

@bot.component("deny")
@bot.component("accept")
async def acception(ctx: interactions.ComponentContext):
    if ctx.component.custom_id == "accept":
        with open("data.json") as file:
            data = json.load(file)
        ctx.callback
        data = data["servers"][ctx.guild_id]["users"][str(ctx.user.id)] = {"group": str(data["servers"][ctx.guild_id]["users"][str(ctx.author.id)]["group"])}
        with open("data.json", 'w') as fout:
            fout.write(json.dumps(indent=4, obj=data))
        ctx.member.add_role(
            role=data["servers"][ctx.guild_id]["users"][str(ctx.member.id)]["group"],
            guild_id=ctx.member.guild_id,
            reason="Accepted invitation from" + str(ctx.member))
    elif ctx.component.custom_id == "deny":
        ctx.member.send(" denied your invitation")

@bot.modal("settings")
async def modal(ctx: interactions.ComponentContext, name1: str, goals: str, color: str):
    print("test")
    role1 = await ctx.guild.create_role(
        name=name1,
        color=int(color, 16),
        hoist=True,
    )
    with open('data.json') as file:
        data = json.load(file)
    if not ctx.guild_id in data["servers"]:
        data["servers"][ctx.guild_id] = {"users": {str(ctx.member.id): {"group": str(role1.id)}}}
    else:
        data["servers"][ctx.guild_id]["users"][str(ctx.member.id)] = {"group": str(role1.id)}
    await ctx.member.add_role(role=role1, guild_id=ctx.guild_id)
    categorie = await ctx.guild.create_channel(
        name=name1,
        type=interactions.ChannelType.GUILD_CATEGORY,
        topic=goals,
        permission_overwrites=[interactions.Overwrite(
        id=role1.id, 
        type=0,
        allow=934158780992,
        ),
        interactions.Overwrite(
        id=ctx.guild_id,
        type=0,
        deny=1024,
        )]
    )


bot.start()

it was working but after I added "/group invite" the "/group create" stopped working the @bot.modal code just wasn't executed and I really don't know why. I couldn't figure out the solution on my own even with the help of a long internet research. I hope some of you know better than me. Thank you in advance!

2

There are 2 best solutions below

1
GFT On

Maybe you should try to change @bot.modal decorator before the @bot.component decorators. Here's the corrected order for your decorators:

# ... (previous code)

@bot.modal("settings")
async def modal(ctx: interactions.ComponentContext, name1: str, goals: str, color: str):
    print("test")
    role1 = await ctx.guild.create_role(
        name=name1,
        color=int(color, 16),
        hoist=True,
    )
    with open('data.json') as file:
        data = json.load(file)
    if not ctx.guild_id in data["servers"]:
        data["servers"][ctx.guild_id] = {"users": {str(ctx.member.id): {"group": str(role1.id)}}}
    else:
        data["servers"][ctx.guild_id]["users"][str(ctx.member.id)] = {"group": str(role1.id)}
    await ctx.member.add_role(role=role1, guild_id=ctx.guild_id)
    categorie = await ctx.guild.create_channel(
        name=name1,
        type=interactions.ChannelType.GUILD_CATEGORY,
        topic=goals,
        permission_overwrites=[interactions.Overwrite(
        id=role1.id, 
        type=0,
        allow=934158780992,
        ),
        interactions.Overwrite(
        id=ctx.guild_id,
        type=0,
        deny=1024,
        )]
    )

@bot.component("deny")
@bot.component("accept")
async def acception(ctx: interactions.ComponentContext):
    # ... (previous code)

# ... (previous code)

bot.start()

Python decorators are applied in the order they are declared, and in your code, you have defined the @bot.component decorators before the @bot.modal decorator. This might be causing the @bot.modal decorator not to be recognized and executed.

0
Luk Petry On

it got fixed when renaming the custom_id of the modal from settings to setting. Don't ask me why. Don't ask me how. But it worked