-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixie.js
More file actions
137 lines (118 loc) · 4.33 KB
/
pixie.js
File metadata and controls
137 lines (118 loc) · 4.33 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
const { Client, GatewayIntentBits, ActivityType, Partials, Collection, REST, Routes } = require('discord.js');
const fs = require('fs');
const toml = require('toml');
const wol = require("./addons/wol.js");
const commands = new Collection();
let config;
try {
const configFile = fs.readFileSync('./config.toml', 'utf8');
config = toml.parse(configFile);
} catch (error) {
console.error('[!!!!!!] error loading config.toml:', error);
process.exit(1);
}
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.DirectMessages,
],
partials: [
Partials.Channel,
Partials.Message
]
});
commands.set('wakeywakey', {
data: {
name: 'wakeywakey',
description: 'wake up computer',
integration_types: [0, 1],
contexts: [0, 1, 2]
},
async execute(interaction) {
if (interaction.user.id !== config.bot.owner_id) {
return interaction.reply({ content: 'You do not have permission to use this command.', ephemeral: true });
}
var sent = wol.wakeOnLan(config.wol.target_mac);
console.log(`[+] wake-on-lan sent via slash command: ${sent}`);
await interaction.reply('okay! waking up computer...');
}
});
// stolen
async function registerCommands() {
const commandsData = Array.from(commands.values()).map(cmd => cmd.data);
const rest = new REST({ version: '10' }).setToken(config.bot.token);
try {
console.log('[...] registering slash commands...');
await rest.put(
Routes.applicationCommands(config.bot.client_id),
{ body: commandsData }
);
console.log('[+] successfully registered slash commands');
} catch (error) {
console.error('[!] error registering commands:', error);
}
}
client.once('clientReady', async () => {
console.log(`[+] logged in as ${client.user.tag}!`);
await registerCommands();
if (config.settings.activity) {
const activityType = ActivityType[config.settings.activity_type] || ActivityType.Playing;
client.user.setActivity(config.settings.activity, { type: activityType });
}
if (config.settings.status) {
client.user.setStatus(config.settings.status);
}
});
// stolen
client.on('interactionCreate', async (interaction) => {
if (!interaction.isChatInputCommand()) return;
const command = commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(`[!] error executing command ${interaction.commandName}:`, error);
const errorMessage = { content: 'There was an error executing this command.', ephemeral: true };
if (interaction.replied || interaction.deferred) {
await interaction.followUp(errorMessage);
} else {
await interaction.reply(errorMessage);
}
}
});
client.on('messageCreate', async (message) => {
if (message.author.bot) return;
if (message.guild) return;
//console.log(`received message from ${message.author.displayName}: ${message.content}`);
if (message.author.id != config.bot.owner_id) return;
if (message.content.toLowerCase().includes("home")) {
var sent = wol.wakeOnLan(config.wol.target_mac);
console.log(`[+] wake-on-lan sent: ${sent}`);
message.channel.send(`welcome back ${message.author.displayName.toLowerCase()}! i'm waking up your computer...`);
}
});
// also nabbed from one of my other bots just to make it a little nicer
client.on('error', (error) => {
console.error('[!] discord client error:', error);
});
process.on('unhandledRejection', (reason, promise) => {
console.error('[!] unhandled rejection at:', promise, 'reason:', reason);
});
process.on('uncaughtException', (error) => {
console.error('[!!!!!!] uncaught exception:', error);
process.exit(1);
});
process.on('SIGINT', () => {
console.log('[...] received SIGINT, shutting down gracefully...');
client.destroy();
process.exit(0);
});
process.on('SIGTERM', () => {
console.log('[...] received SIGTERM, shutting down gracefully...');
client.destroy();
process.exit(0);
});
client.login(config.bot.token);