#Hikari Slash Command
1 messages · Page 1 of 1 (latest)
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
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
what should i do
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)
i paste this into my ping.py and change "name" with "ping" but my bot still dont have ping command
did you load the plugin?
try reloading discord
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)
np
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)```
read the error
don't load an extension that's already loaded 
but i have 1 ban command only
i fixed it
you should reboot your bot

show your code
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
load your extensions with client.load_extensions_from("./cogs/")
ı do it
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
read the docs on how load_extensions_from works https://hikari-lightbulb.readthedocs.io/en/latest/api_references/app.html#lightbulb.app.BotApp.load_extensions_from
Thanks
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
Oh ok thanks
np!
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
- how can i want a member with member option like when i in member option, option shows me the member
- how can i change reason and members places i want to ask member first
Decorators are read from bottom to top, so swap the order of them
@lightbulb.option has a type param, set it to hikari.Member
./tag tias
ok than its not worked
sorry, that's my bad - it should be hikari.OptionType.USER
ok thanks
how can i get a members highest role
await ctx.send("You cannot kick this user", ephemeral=True)
return```
like this
?