Git Product home page Git Product logo

discord-handlers's Introduction

Discord-Handlers

Creative Commons License

This Module is Licensed Under the CC-BY-NC-SA 4.0 License. A Copy of the License is Found Here, As Well As An Easier-to-Read Summary here


About

discord-handlers Is A Node.JS Module, Compatible With JavaScript & TypeScript, Assisting With Creating REST & Non-REST Event Handlers, As Well As Assisting With Creating Slash Commands For Your Discord bot

  • Compatible With (And Uses) Discord.JS
  • Simple and Easy to Use

Installation

NPM:

npm i @aspnyan/discord-handlers

or

npm i https://github.com/ASPNyan/discord-handlers

Yarn:

yarn add @aspnyan/discord-handlers

or

yarn add https://github.com/ASPNyan/discord-handlers

Setup

(Make Sure You've Already Installed Discord.JS Before Starting This. If you haven't, then run npm i discord.js)

Initial Setup

Do this in your main file (usually index.js or index.ts)

JavaScript:

  • CommonJS:
const { Client, Collection } = require("discord.js");
const CMDManager = require("@aspnyan/discord-handlers");
const client = new Client({
  intents: ["Guilds", "GuildMessages"],
});

client.login("token");

export const CMD = new CMDManager(client, {
  CommandDirectory: "./Commands", // This Doesn't Have to be "./Commands", Just Make Sure It Starts With "./"
  EventsDirectory: "./Events", // This Doesn't Have to be "./Events", Just Make Sure It Starts With "./"
  DeveloperServerID: "dev_server_id",
  FileExtension: "CommonJS",
});
  • ES6:
import { Client, Collection } from "discord.js";
import CMDManager from "@aspnyan/discord-handlers";
const client = new Client({
  intents: ["Guilds", "GuildMessages"],
});

client.login("token");

export const CMD = new CMDManager(client, {
  CommandDirectory: "./Commands", // This Doesn't Have to be "./Commands", Just Make Sure It Starts With "./"
  EventsDirectory: "./Events", // This Doesn't Have to be "./Events", Just Make Sure It Starts With "./"
  DeveloperServerID: "dev_server_id",
  FileExtension: "ECMAScript",
});

TypeScript:

import { Client, Collection } from "discord.js";
import CMDManager from "@aspnyan/discord-handlers";
const client = new Client<boolean>({
  intents: ["Guilds", "GuildMessages"],
});

client.login("token");

export const CMD = new CMDManager(client, {
  CommandDirectory: "./Commands", // This Doesn't Have to be "./Commands", Just Make Sure It Starts With "./"
  EventsDirectory: "./Events", // This Doesn't Have to be "./Events", Just Make Sure It Starts With "./"
  DeveloperServerID: "dev_server_id",
  FileExtension: "TypeScript",
});

Events

Make a new JS/TS file in the folder you stated in your CMDManager Events Directory. We're Making an Event for "interactionCreate" (We'll need it soon), so call it that, not that it matters.

You should now have a file tree similar to this:

Main Folder
|
|- Events -|
|          interactionCreate.js (or .ts for TypeScript)
|
|- index.js (or .ts)

Go into the file, and paste the code block of the language you're using

JavaScript:

  • CommonJS:
const { CMD } = require("../index");

module.exports = {
  name: "interactionCreate",
  once: false,
  rest: false,
  execute: (interaction) => {
    const commands = CMD.CommandCollection;
    const executions = CMD.ExecCollection;
    if (interaction.isChatInputCommand()) {
      const command = commands.get(interaction.commandName);
      if (!command) return;
      const execute = executions.get(interaction.commandName);
      if (!execute || !execute.execute) return;
      execute.execute(interaction.client, interaction, interaction.guild);
    }
  },
};
  • ES6:
import { EventInterface } from "@aspnyan/discord-handlers";
import { CMD } from "../index";

export default {
  name: "interactionCreate",
  once: false,
  rest: false,
  execute: (interaction) => {
    const commands = CMD.CommandCollections;
    const executions = CMD.ExecCollections;
    if (interaction.isChatInputCommand()) {
      const command = commands.get(interaction.commandName);
      if (!command) return;
      const execute = executions.get(interaction.commandName);
      if (!execute || !execute.execute) return;
      execute.execute(interaction.client, interaction, interaction.guild);
    }
  },
};

TypeScript:

import {
  EventInterface,
  CommandJSONExport,
  CommandInterface,
} from "@aspnyan/discord-handlers";
import { CMD } from "../index";

export default {
  name: "interactionCreate",
  once: false,
  rest: false,
  execute: (interaction) => {
    const commands = CMD.CommandCollections;
    const executions = CMD.ExecCollections;
    if (interaction.isChatInputCommand()) {
      const command: CommandJSONExport | undefined = commands.get(
        interaction.commandName
      );
      if (!command) return;
      const execute: CommandInterface | undefined = executions.get(
        interaction.commandName
      );
      if (!execute || !execute.execute) return;
      execute.execute(interaction.client, interaction, interaction.guild);
    }
  },
} as EventInterface;

Slash Commands

Make a new JS/TS file in the folder you stated in your CMDManager Commands Directory. We're making a basic "ping" command so call it that, not that it matters.

You should now have a file tree similar to this:

Main Folder
|
|- Events -|
|          interactionCreate.js (or .ts for TypeScript)
|
|- Commands -|
|            ping.js (or .ts for TypeScript)
|
|- index.js (or .ts)

Go into the file, and paste the code block of the language you're using

JavaScript:

  • CommonJS:
module.exports = {
  name: "ping",
  description: "Pong!",
  dm_permission: true,
  execute: (client, interaction, guild) => {
    interaction.reply({
      content: "Pong!",
      ephemeral: true,
    });
  },
};
  • ES6:
export default {
  name: "ping",
  description: "Pong!",
  dm_permission: true,
  execute: (client, interaction, guild) => {
    interaction.reply({
      content: "Pong!",
      ephemeral: true,
    });
  },
};

TypeScript:

import { CommandInterface } from "@aspnyan/discord-handlers";

export default {
  name: "ping",
  description: "Pong!",
  dm_permission: true,
  execute: (client, interaction, guild) => {
    interaction.reply({
      content: "Pong!",
      ephemeral: true,
    });
  },
} as CommandInterface;

There's a lot more you can do with Slash Commands and Events that isn't explained here, so make sure to look through all of that. If I have time I might write some documentation for everything, but don't expect it yet.


License

This Module is Licensed Under A Modified Version of the CC-BY-NC-SA 4.0 License. A Copy of the License is Found Here.

Creative Commons License

The following is a human-readable summary of (and not a substitute for) the license.

--Start--

You are free to:

  • Share — copy and redistribute the material in any medium or format

  • Adapt — remix, transform, and build upon the material

  • The licensor cannot revoke these freedoms as long as you follow the license terms.


Under the following terms:

  • Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.

  • NonCommercial — You may not use the material for commercial purposes.

  • ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.

  • No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits.


Notices:

  • You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation.
  • No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material.

--End--

To Contact Me About Commercial Use, You Can DM Me on Discord @ ASP Nyan#1169 (User ID: 387139715301769216). I am a Part of the Official Discord.JS Discord Server and The Coding Den Discord Server.

discord-handlers's People

Contributors

aspnyan avatar

Watchers

 avatar

discord-handlers's Issues

!! Incompatible with CJS & MJS ATM !!

I'm a dumbass. Is all.
(In all honesty, I forgot that my instructions specify .mjs and .cjs for file extensions, this is being worked on as that doesn't work)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.