#command?

1 messages · Page 1 of 1 (latest)

ebon lagoon
#

what do you need help with?

#

@barren wolf?

barren wolf
#

i want to know what i would have to do to store the number in the message as a variable to use later for the amount of messages to bulk delete

ebon lagoon
#

okay so

barren wolf
#

so

ebon lagoon
#

const args = message.content.slice(prefix.length).trim().split(/ +/);

#

you need this line for getting args

#

what it does is

#

message.content returns the message that was sent

#

so like if i were to send !hello

barren wolf
#

  if (message.content.startsWith("H!clear ")){

    

  }``` this is my code btw
ebon lagoon
#

it will return !hello

ebon lagoon
ebon lagoon
#

if (!message.content.startsWith(prefix) || message.author.bot) return;

#

this line checks if the message

#

doesnt start with your prefix

#

or

#

is from a bot

barren wolf
#

mhm

ebon lagoon
#

and will return

#

so the commands dont get ran

#

const args = message.content.slice(prefix.length).trim().split(/ +/);

#

this line

#

what is does it

#

lets say

#
const prefix = "H!"
#

your prefix is this

barren wolf
#

yes it is

ebon lagoon
#

so

#

message.content.slice()

#

what is does is

barren wolf
#

slices the message content?

ebon lagoon
#

it takes an integer as the argument

ebon lagoon
#

it slices the front

#

so

#

lets say

#
let content = "H!command"
#

your content is this

#

if you do

barren wolf
#

mhm

ebon lagoon
#

content.slice(2)

barren wolf
#

it will take off

ebon lagoon
#

it will return "command"

barren wolf
#

the prefix

#

yes

ebon lagoon
#

yup

barren wolf
#

so

#

i want to do a clear

ebon lagoon
#

so now we have

#

.trim()

barren wolf
#

so i would do

ebon lagoon
#

ok so now

#

trim() just removes trailing spaces

#

.split() splits by space

#

so now

#

we have

#

H!clear 10

barren wolf
#

yes

ebon lagoon
#

args will return ["clear', "10"]

#

your command will be

#

args.shift()

#

which removes the first element and returns it

#

so

#

now we do

barren wolf
#

couldnt i just slice clear too?

ebon lagoon
#

so now

barren wolf
#

dokily

ebon lagoon
#

slice will return an array

#

shift will return the element

#

which is a string

barren wolf
#

ok

ebon lagoon
#

so your command

#
if command === "clear" {}
#

to get the number

#

you do

#
const toClear = parseInt(args[1])
#

parseInt returns a number from a string

#

so now

barren wolf
#

ok

ebon lagoon
#

you want to get the messages

barren wolf
#

mhm

ebon lagoon
#

so you do

barren wolf
#

i do

ebon lagoon
#

wait

barren wolf
#

ok

ebon lagoon
#

im looking at the docs for the method lol

#

message.channel.messages

barren wolf
#

lol

ebon lagoon
#

returns the MessageManager

barren wolf
#

couldnt you just do

ebon lagoon
#

?

barren wolf
#

channel.bulkDelete(n)

ebon lagoon
#

no

barren wolf
#

why not

ebon lagoon
#

wait you can

#

sorry

barren wolf
#

lol

ebon lagoon
#

yaeh just do that

barren wolf
#

okily dokily

ebon lagoon
#

my bad ahah

barren wolf
#

so

#

what do i write

#

lol

ebon lagoon
#

message.channel.bulkDelete(toClear)

#

will clear x amount of messages from the channel that the command was sent in

barren wolf
#

yes

#

but to get the number, would i do this?
message.content.slice(prefix.length).trim().split(/ +/);

ebon lagoon
#

no that returns your command + arguments

#

so like

barren wolf
#

oh

ebon lagoon
#

H!clear 10

#

that will return

barren wolf
#

yesp

ebon lagoon
#

["clear", "10"]

#

yeah

barren wolf
#

mhm

#

so would this code be alright

#

or no

ebon lagoon
#

no

#

you need to store the thing in a var

#
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');

const client = new Discord.Client();

client.once('ready', () => {
    console.log('Ready!');
});

client.on('message', message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).trim().split(/ +/);
    const command = args.shift().toLowerCase();

    if (command === 'ping') {
        message.channel.send('Pong.');
    } else if (command === 'beep') {
        message.channel.send('Boop.');
    }
    // ...
});

client.login(token);

#

this is a good example

barren wolf