#Hikari Slash Command

1 messages · Page 1 of 1 (latest)

verbal light
#

you have:

  • plugins in lightbulb
  • components in tanjun
  • plugins in crescent
#

each command handler has it's own examples of how to use them

carmine oxide
#

my main.py

import lightbulb
import os
from setting import token

client = lightbulb.BotApp(token=token, intents=hikari.Intents.ALL)

@client.listen(hikari.ShardReadyEvent)
async def on_ready(event: hikari.ShardReadyEvent) -> None:
    print(f"Logged in as {event.my_user}")

for fn in os.listdir("./cogs"):
    if fn.endswith(".py"):
        client.load_extensions(f"cogs.{fn[:-3]}")

client.run()```

and in my cogs file i have ping.py
```import hikari
import lightbulb

class Ping(lightbulb.Plugin):
    @lightbulb.command("ping","pong")
    @lightbulb.implements(lightbulb.SlashCommand)
    async def ping(self, ctx: lightbulb.Context) -> None:
        await ctx.respond("Pong!")

def load(client: lightbulb.BotApp) -> None:
    client.add_plugin(Ping("ping"))```
#

what is wrong

#

@verbal light

verbal light
#

use client.load_extensions_from("./cogs/") instead

#

that'll load all the .py files in the cogs folder (that don't start with an underscore)

#

and your command is wrong

carmine oxide
verbal light
#

it's not supposed to be a class

#

a sample plugin is this:```py
import hikari
import lightbulb

plugin = lightbulb.Plugin("name")

@plugin.command
@lightbulb.command("ping", "pong")
@lightbulb.implements(lightbulb.SlashCommand)
async def ping(self, ctx: lightbulb.Context) -> None:
await ctx.respond("Pong!")

def load(client: lightbulb.BotApp) -> None:
client.add_plugin(plugin)

carmine oxide
verbal light
#

did you load the plugin?

carmine oxide
verbal light
#

try reloading discord

carmine oxide
verbal light
#

oh my bad

#

this is wrong

async def ping(self, ctx: lightbulb.Context) -> None:```
#

it should have been ```py
async def ping(ctx: lightbulb.Context) -> None:

#

(no self parameter because it's no a class)

carmine oxide
#

oh thanks

#

its work now

verbal light
#

np

carmine oxide
#
import lightbulb

plugin = lightbulb.Plugin("ban")

@plugin.command
@lightbulb.command("ban", "ban")
@lightbulb.has_guild_permissions(hikari.Permissions.BAN_MEMBERS)
@lightbulb.option("user", "The user to ban", converters=hikari.Member)
@lightbulb.option("reason", "The reason for the ban", default="No reason provided")
@lightbulb.implements(lightbulb.SlashCommand)
async def ban(ctx: lightbulb.Context) -> None:
    await ctx.respond(f"Banned {ctx.options['user'].mention} for {ctx.options['reason']}")
    await ctx.options["user"].ban(reason=ctx.options["reason"])

def load(client: lightbulb.BotApp) -> None:
    client.add_plugin(plugin)```
verbal light
#

read the error

carmine oxide
#

sorry

verbal light
#

don't load an extension that's already loaded catThink

carmine oxide
#

but i have 1 ban command only

stiff moss
carmine oxide
stiff moss
#

you should reboot your bot

stiff moss
carmine oxide
#

i cant fix it :/

burnt stag
#

show your code

carmine oxide
#

import hikari
import lightbulb

plugin = lightbulb.Plugin("ban")

@plugin.command
@lightbulb.command("ban","ban")
@lightbulb.implements(lightbulb.SlashCommand)
async def ban(ctx: lightbulb.Context, member: hikari.Member, reason: str) -> None:
await member.ban(reason=reason)
await ctx.respond(f"Banned {member} for {reason}", ephemeral=True)

def load(client: lightbulb.BotApp) -> None:
client.add_plugin(plugin)

#

import hikari
import lightbulb
import os
from setting import token

client = lightbulb.BotApp(token=token, intents=hikari.Intents.ALL, help_slash_command=True)

@client.listen(hikari.ShardReadyEvent)
async def on_ready(event: hikari.ShardReadyEvent) -> None:
print(f"Logged in as {event.my_user}")

for fn in os.listdir("./cogs"):
if fn.endswith(".py"):
client.load_extensions_from("./cogs/")

client.run()

#

@burnt stag

verbal light
carmine oxide
#

ı do it

burnt stag
#

yes, but you don't need to iterate over the directory

#

lightbulb will do that for you

#

take out the for fn in os.listdir(...) and the if fn.endswith and just have client.load_extensions_from

#

assuming you have more than one file in cogs (which seems to be the case):
the first iteration will load all your plugins, i.e plugin1 and plugin2
the second iteration will load all your plugins again, but fail, because it cannot load what is already loaded

carmine oxide
#

Thanks

burnt stag
#

also (and feel free to ignore this) - the nice thing about hikari is you could realistically have whatever folder/file structure you want, i.e you don't need to name your plugins/extensions folder cogs :) there's a lot of things that d.py makes you do that you don't actually have to do with hikari and the addon libraries

carmine oxide
#

Oh ok thanks

burnt stag
#

np!

carmine oxide
#
import lightbulb
from settings import logChannel

plugin = lightbulb.Plugin("ban")

@plugin.command
@lightbulb.option("member","You can ban a member with this command", required=True)
@lightbulb.option("reason","You can ban a member with this command", required=True)
@lightbulb.command("ban","You can ban a member with this command")
@lightbulb.implements(lightbulb.SlashCommand)
async def ban(ctx: lightbulb.Context) -> None:
    await ctx.options.member.ban(reason=ctx.options.reason)
    await ctx.respond(f"Banned {ctx.options.member} for {ctx.options.reason}", ephemeral=True)
    embed = hikari.Embed(title="User Banned", description=f"User: {ctx.options.member}\nAdmin: {ctx.user.mention}\nReason: {ctx.options.reason}", color=0x00FF00)
    channel = await ctx.bot.rest.fetch_channel(logChannel)
    await channel.send(embed=embed)

def load(client: lightbulb.BotApp) -> None:
    client.add_plugin(plugin)```
#

i have 2 question

  1. how can i want a member with member option like when i in member option, option shows me the member
  2. how can i change reason and members places i want to ask member first
burnt stag
#

Decorators are read from bottom to top, so swap the order of them

#

@lightbulb.option has a type param, set it to hikari.Member

carmine oxide
#

like this

burnt stag
#

./tag tias

oblique abyssBOT
carmine oxide
#

ok than its not worked

burnt stag
#

sorry, that's my bad - it should be hikari.OptionType.USER

carmine oxide
#

ok thanks

carmine oxide
#

how can i get a members highest role

#
        await ctx.send("You cannot kick this user", ephemeral=True)
        return```
#

like this

carmine oxide
#

?