I'am currently writing a Discord bot. It accepts the level of a user via a command and then outputs a ASCII table where one can see the level of every user. I got the part with the commands but I'm having problems with the ASCII table.
I'm using the ascii-table module to do the formatting of the table.
My code should in theory work but it always gives me the error TypeError: <something> is not a function
when it comes to the part where the stuff from the ascii-table module is used.
I couldn't find a solution with google so I hope someone of you could help me.
My Code: mybot.js
const Discord = require('discord.js');
const client = new Discord.Client();
const config = require('./config.json');
const fs = require('fs');
const AsciiTable = require('ascii-table');
let level = JSON.parse(fs.readFileSync('./level.json', 'utf8'));
client.login(config.token);
client.on('ready', () => {
console.log('I am ready!');
});
client.on('message', (message) => {
if (!message.content.startsWith(config.prefix) || message.author.bot) return;
if (message.content.startsWith(config.prefix + 'register')) {
let args = message.content.split(' ');
if (!level.user[message.author.id]) level.user = {
[message.author.id]: {
username: '',
levels_EN: 0,
levels_LU: 0,
levels_NO: 0,
levels_SC: 0,
levels_SW: 0
}
};
level.user[message.author.id].username = args[1];
fs.writeFile('./level.json', JSON.stringify(level), (err) => {
if (err) console.error(err)
});
message.channel.send('Registration successful as: *' + args[1] + '*');
} else
if (message.content.startsWith(config.prefix + 'level')) {
if (!level.user[message.author.id]) {
message.channel.send('Not yet registered. Please register using ```!register <username>```');
return
}
let args = message.content.split(' ');
if (!args[1].match(/^\d+$/)) {
if (args[1] === "EN" || args[1] === "Engineers") {
level.user[message.author.id].levels_EN = args[2];
message.channel.send('Engineers has been set to: *' + args[2] + '*');
} else
if (args[1] === "LU" || args[1] === "Lunatics") {
level.user[message.author.id].levels_LU = args[2];
message.channel.send('Lunatics has been set to: *' + args[2] + '*');
} else
if (args[1] === "NO" || args[1] === "Nomads") {
level.user[message.author.id].levels_NO = args[2];
message.channel.send('Nomads has been set to: *' + args[2] + '*');
} else
if (args[1] === "SC" || args[1] === "Scavengers") {
level.user[message.author.id].levels_SC = args[2];
message.channel.send('Scavengers has been set to: *' + args[2] + '*');
} else
if (args[1] === "SW" || args[1] === "Steppenwolfs") {
level.user[message.author.id].levels_SW = args[2];
message.channel.send('Steppenwolfs has been set to: *' + args[2] + '*');
} else {
message.channel.send('Could not recognize the argument. Please use ```!level <faction> <#>``` to set one level or ```!level <EN#> <LU#> <NO#> <SC#> <SW#>``` to set all levels at once.');
}
fs.writeFile('./level.json', JSON.stringify(level), (err) => {
if (err) console.error(err)
});
return
}
level.user[message.author.id].levels_EN = args[1];
level.user[message.author.id].levels_LU = args[2];
level.user[message.author.id].levels_NO = args[3];
level.user[message.author.id].levels_SC = args[4];
level.user[message.author.id].levels_SW = args[5];
message.channel.send('Engineers has been set to: *' + args[1] + '*');
message.channel.send('Lunatics has been set to: *' + args[2] + '*');
message.channel.send('Nomads has been set to: *' + args[3] + '*');
message.channel.send('Scavengers has been set to: *' + args[4] + '*');
message.channel.send('Steppenwolfs has been set to: *' + args[5] + '*');
fs.writeFile('./level.json', JSON.stringify(level), (err) => {
if (err) console.error(err)
});
} else
if (message.content.startsWith(config.prefix + 'ping')) {
message.channel.send('pong!');
} else
if (message.content.startsWith(config.prefix + 'json')) { //This is where the table should be generated
var userlist = Object.keys(level.user)
let times = userlist.length;
message.channel.send(userlist.length);
message.channel.send(userlist);
message.channel.send(typeof userlist);
var stattable = new AsciiTable("Crossout Levels")
stattable
.removeBorder
.setHeading('User', 'Engineers', 'Lunatics', 'Nomads', 'Scavengers', 'Steppenwolfs'); //And here it always gives me the error
for (var i = 0; i < times; i++) {
var userinfo = Object.keys(level.user[userlist[0]]);
stattable
.addRow(userinfo[0], userinfo[1], userinfo[2], userinfo[3], userinfo[4], userinfo[5])
delete userlist[0]
if (i === times) {
message.channel.send('```' + stattable + '```')
}
}
} else
if (message.content.startsWith(config.prefix + 'prefix')) {
let newPrefix = message.content.split(' ').slice(1, 2)[0];
config.prefix = newPrefix;
fs.writeFile('./config.json', JSON.stringify(config), (err) => {
if (err) console.error(err)
});
}
});
client.on('error', (e) => console.error(e));
client.on('warn', (e) => console.warn(e));
client.on('debug', (e) => console.info(e));
level.json
This is the data I want to put in the table.
{
"user": {
"000000000000000000": {
"username": "Test_0",
"levels_EN": "9",
"levels_LU": "8",
"levels_NO": "7",
"levels_SC": "6",
"levels_SW": "5"
},
"111111111111111111": {
"username": "Test_1",
"levels_EN": "1",
"levels_LU": "2",
"levels_NO": "3",
"levels_SC": "4",
"levels_SW": "5"
},
"222222222222222222": {
"username": "Test_2",
"levels_EN": "11",
"levels_LU": "22",
"levels_NO": "33",
"levels_SC": "44",
"levels_SW": "55"
}
}
}
The full error message:
C:\Users\Konrad\Desktop\HTML\Discord\Crossout\mybot.js:114
.setHeading('User', 'Engineers', 'Lunatics', 'Nomads', 'Scavengers', 'Steppenwolfs');
^
TypeError: stattable.removeBorder.setHeading is not a function
at Client.client.on (C:\Users\Konrad\Desktop\HTML\Discord\Crossout\mybot.js:114:14)
at emitOne (events.js:96:13)
at Client.emit (events.js:188:7)
at MessageCreateHandler.handle (C:\Users\Konrad\Desktop\HTML\Discord\Crossout\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
at WebSocketPacketManager.handle (C:\Users\Konrad\Desktop\HTML\Discord\Crossout\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:102:65)
at WebSocketConnection.onPacket (C:\Users\Konrad\Desktop\HTML\Discord\Crossout\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:325:35)
at WebSocketConnection.onMessage (C:\Users\Konrad\Desktop\HTML\Discord\Crossout\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:288:17)
at WebSocket.onMessage (C:\Users\Konrad\Desktop\HTML\Discord\Crossout\node_modules\ws\lib\EventTarget.js:103:16)
at emitTwo (events.js:106:13)
at WebSocket.emit (events.js:191:7)
The ascii-table module can be found here: https://github.com/sorensen/ascii-table
In case something is unclear or you to see a file please tell me, I will try to as much information as I can.
via Konrad Stüwe
No comments:
Post a Comment