Skip to content

Instantly share code, notes, and snippets.

@Captain8771
Last active August 22, 2022 17:49
Show Gist options
  • Save Captain8771/869e6c64361da9b14f752ebfbfdb0df4 to your computer and use it in GitHub Desktop.
Save Captain8771/869e6c64361da9b14f752ebfbfdb0df4 to your computer and use it in GitHub Desktop.
Powercord snippet for adding and removing built in slash commands

Snippet

let { getModule, messages: { receiveMessage } } = require("powercord/webpack")
let { createBotMessage } = getModule(["createBotMessage"], false)


function CreateCommand(name, description, options, callback, applicationId="-1") {
    getModule(["getBuiltInCommands"], false).BUILT_IN_COMMANDS.push({
        applicationId: applicationId,
        description: description,
        displayDescription: description,
        displayName: name,
        execute: (args, context) => {
            let res = callback(args, context)
            if (res) {
                let msg = createBotMessage({channelId: context.channel.id, content: res.value})
                receiveMessage(context.channel.id, msg)
            }
        },
        id: `-${getModule(["getBuiltInCommands"], false).BUILT_IN_COMMANDS.length + 1}`,
        inputType: 0,
        name: name,
        options: options,
        type: 1,
    })
}

function RemoveCommand(name) {
    let cmds = getModule(["getBuiltInCommands"], false).BUILT_IN_COMMANDS
    cmds.pop(cmds.indexOf(cmds.find(i => i.name.toLowerCase() == name.toLowerCase())))
}

Adding a slash command

// syntax: (name, description, options[], callback)
CreateCommand("test", "test command", [{name: "test", displayName: "test", type: 3, required: true, description: "test", displayDescription: "test"}], (args, context) => {return {value: args[0].value}})

Removing a slash command

// syntax: (name)
RemoveCommand("test")
Copy link

ghost commented May 21, 2022

imo the command id could probably be done better, like with the value being similar to the below:

...
id: `-${getModule(["BUILT_IN_COMMANDS"]).BUILT_IN_COMMANDS.length + 1}`,
...

but other than that it's great

oh also an option to change applicationids for cmds (-1 is default discord app commands, the others are something that i dont remember and im too lazy to check rn) would be pretty cool

@Captain8771
Copy link
Author

I've applied the suggested changes, thanks!

Copy link

ghost commented May 22, 2022

👍

@Vap0r1ze
Copy link

Vap0r1ze commented Aug 8, 2022

cmds.pop(cmds.indexOf(cmds.find(i => i.name.toLowerCase() == name.toLowerCase())))

Array::pop doesn't take an index, try using Array::splice:

let cmdIndex = cmds.findIndex(i => i.name.toLowerCase() == name.toLowerCase())
if (cmdIndex >= 0) cmds.splice(cmdIndex, 1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment