This repository was archived by the owner on Jun 28, 2024. 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
186 lines (175 loc) · 5.88 KB
/
init.ts
File metadata and controls
186 lines (175 loc) · 5.88 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { Message, Client, Collection, Intents } from 'discord.js'
import Trollsmile from 'trollsmile-core'
import { CommandObj } from './utils/types'
import process from 'process'
import path, { basename } from 'path'
import { existsSync as exists, readFileSync as read_file } from 'fs'
import { createServer as server } from 'http'
import { recursive_readdir } from './utils/rreaddir.js'
import fetch from 'node-fetch'
import { all } from './messages.js'
import { isMain as is_main } from './utils/isMain.js'
import 'discord-reply'
import buttons from 'discord-buttons'
// setup ffmpeg
import ffmpegpath from 'ffmpeg-static'
import fluent from 'fluent-ffmpeg'
fluent.setFfmpegPath(ffmpegpath)
// So some files don't import node-fetch
// I actually don't know how it happened
// But just in case
// we set fetch as a global variable
globalThis.fetch = fetch as any
// so i feel that [1,2,3,4].random() is nicer than random([1,2,3,4]) i guess
globalThis.Array.prototype.random = function () {
return this[Math.floor(Math.random() * this.length)]
}
/**
* "trollsmile winning" - LuaQuack
*
* The Bot class is a thin wrapper around trollsmile-core that makes it function on Discord.js.
* Get the client with this.client.
*
* @since 0.0.1
* @license ISC
* @author Jack W. <hello@5079.ml> (https://5079.ml)
*/
class Bot extends Trollsmile<Message, CommandObj> {
filter(message: Message) {
return !message.author.bot
}
client: Client
commands = new Collection<string, CommandObj>()
constructor(prefix: string) {
super(prefix)
this.client = new Client({
ws: {
intents: [Intents.NON_PRIVILEGED]
}
})
buttons(this.client) // add button support
this.client.on('message', (message) => {
this.emit('message', message)
})
this.client.on('ready', () => {
this.activityChanger(900000)
})
this.on('output', ([out, message]) => {
// @ts-expect-error
message.lineReply(out)
})
this.client.on('error', console.error.bind(console, '[DISCORD ERROR]'))
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.client.user?.avatarURL() || undefined
},
title: err.toString(),
color: 'RED'
}
})
})
this.client.login() // process.env.DISCORD_TOKEN
this.load_cmds()
}
/**
* Reads the commands folder and imports the command and sets aliases
* Also logs progress (SIDE EFFECT HOW COOL HOW FUCKING COOL)
* trollsmile currently only supports JS modules
* I wanna add support for another language though that would be pretty funny
*/
async load_cmds() {
const files_in_commands = await recursive_readdir('./commands/')
const commands = files_in_commands
.map((file) => path.resolve(file))
.filter((file) => file.endsWith('.js'))
commands.forEach(async (file, i) => {
const command_name = basename(file, '.js')
console.log('Importing', command_name, `${i + 1}/${commands.length}`)
// we use cache= to bypass Node's module caching
// this allows -update to work
// because if we didn't
// it would import the version of the module before the update
// which would ruin the point of -update
const command: CommandObj = Object.assign(
{ path: 'file://' + file },
await import('file://' + file + `?cache=${Math.random()}`)
)
command.aliases?.forEach((alias: string) => {
this.aliases.set(alias, command_name)
})
this.commands.set(command_name, command)
})
}
/**
* trollsmile funny playing
*
* @param ms The amount of time in milliseconds trollsmile should wait before updating the activity again
*/
activityChanger(ms: number) {
// activityChanger from esmBot, also known as "the gamer code"
const { type, line } = all.random()
console.log('trollsmile:', line)
this.client.user!.setActivity(line, {
type
})
setTimeout(() => this.activityChanger(ms), ms)
}
}
// //====================================================\\
// LOADER
// For when trollsmile is ran directly.
// \\====================================================//
if (is_main(import.meta)) {
/*
trollsmile is hosted on replit
so it would go to sleep after 5 minutes
so i used cron-job to ping it every 5 minutes
but obviously to ping it i need a web server
This wouldn't be a problem if I was allowed to host my bot myself but that breaks Verizon TOS
(page 23 of
https://www.verizon.com/about/sites/default/files/Verizon-Customer-Agreement-Int-TV-Voice-Prepaid-031221.pdf
says "You may not[...]knowingly or unknowingly use the Services to host any type of server")
so this is what i do to detect it
*/
if (process.env.REPLIT_DB_URL) {
server((_, res) => {
res.writeHead(200, {
'Content-Type': 'text/html'
})
res.write(
`<meta http-equiv="refresh" content="0;url=https://troIIsmile.github.io">`
)
res.end()
}).listen(8080)
}
// dotenv implementation
// because i don't like having more modules installed™
// it don't support quotes but who tf cares
if (exists('./.env')) {
Object.assign(
process.env,
Object.fromEntries(
read_file('./.env', 'utf-8')
.split('\n')
.filter((line) => !line.startsWith('#') && line)
.map((line) => line.split('='))
)
)
}
// OOPS! All Side Effects
// also i want a dev prefix
// so to detect Glitch/replit I do this terrible thing
// and also if someone is using a sane hosting platform i also check NODE_ENV
new Bot(
process.env.REPLIT_DB_URL ||
process.env.PORT ||
process.env.NODE_ENV === 'production'
? '-'
: 't!'
)
}
export default Bot