This repository was archived by the owner on Mar 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.ts
More file actions
91 lines (85 loc) · 2.57 KB
/
init.ts
File metadata and controls
91 lines (85 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { Client, Collection, Intents, Message } from 'discord.js'
import {
existsSync as exists,
readFileSync as readFile,
readdirSync
} from 'fs'
import { ServerResponse, createServer } from 'http'
import { homepage, bugs } from './package.json'
import { join } from "path"
import Trollsmile from 'trollsmile-core'
import { CommandObj } from "./utils/types"
globalThis.fetch = require('node-fetch') // shit workaround in case i missed anything
globalThis.Array.prototype.random = function () {
return this[Math.floor(Math.random() * this.length)]
}
// dotenv support
if (require.main === module && exists('./.env') ) {
Object.assign(process.env,
Object.fromEntries(
// Overwrite the env with the .env file
readFile('./.env', 'utf-8')
.split('\n') // split the file into lines
.filter(line => !line.startsWith('#') && line) // remove comments and spacing
.map(line => line.split('=')) // split the lines into key:value pairs
))
}
class Bot extends Trollsmile<Message, CommandObj> {
filter = (msg: Message) => !msg.author.bot
commands = new Collection<string, CommandObj>()
client: Client
constructor(prefix: string, token = process.env.TOKEN) {
super(prefix)
this.client = new Client({
ws: {
intents: [Intents.NON_PRIVILEGED]
}
})
this.on('output', ([out, message]) => {
message.channel.send(out)
})
// Load in events
readdirSync(join(__dirname, '/events/'))
.filter(name => name.endsWith('.js'))
.map(name => name.replace('.js', ''))
.forEach(async filename => {
const ev = (await import(join(__dirname, '/events/', filename))).default
this.client.on(filename, context => {
ev.call(this, context)
})
})
this.client.login(token)
this.on('error', ([err, message]) => {
message.channel.stopTyping()
message.channel.send({
embed: {
author: {
name: `${this.client.user?.username} ran into an error while running your command!`,
// iconURL: this.user?.avatarURL()
},
title: err.toString(),
color: 'RED',
footer: {
text: `Report this bug @ ${bugs}`
}
}
})
})
}
}
if (require.main === module) {
new Bot('-')
}
// replit redirect
if (process.env.REPLIT_DB_URL) {
createServer((_, res: ServerResponse) => {
res.writeHead(200, {
'Content-Type': 'text/html'
})
res.write(
`<meta http-equiv="refresh" content="0;url=${homepage}">`
)
res.end()
}).listen(8080)
}
export default Bot