This repository was archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.ts
105 lines (95 loc) · 3.74 KB
/
Main.ts
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
/*!
* Callisto, a simple and powerful bot package for nin0-dev's chat server.
* Copyright (c) 2022 Kodarru and contributors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// ******
// Import the necessary modules.
import { Client, type Callisto } from "../Source/Main.ts";
// ******
const client = new Client({
Bot: {
username: "YOUR_BOT_NAME",
key: "YOUR_BOT_KEY",
// Has no use implemented yet. Leave it as an empty array.
staleMessages: [],
},
Websocket: {
URI: "wss://guhws.nin0.dev",
Reconnect: true,
ReconnectMaxTrials: 5,
},
LogLevel: 2,
});
const Log = client.Logger;
let prefix = "!";
// This function will be called when the Websocket is opened.
client.On("open", () => {
Log.Info("Connection established!");
client.Send("Hello, world! My prefix is " + prefix + ". Type " + prefix + "help for a list of commands.");
});
// This function will be called when the Websocket receives a message.
client.On("message", (message: Callisto.Message) => {
// Will not respond to bots.
if (message.isBot()) return;
// Will not respond to itself.
if (message.isSelf()) return;
/*
* The message object has the following functions:
*
* isMod(): boolean; - Checks if the message was sent by a admin/mod.
* isBot(): boolean; - Checks if the message was sent by a bot.
* isDiscord(): boolean; - Checks if the message was sent from Discord.
* isUser(): boolean; - Checks if the message was sent by a user.
* isSelf(): boolean; - Checks if the message was sent by the bot.
* Send(content: string): void; - Sends a message to the chat.
* Reply(content: string): void; - Replies to the user who sent the message.
*/
// Check if the message starts with the prefix.
if (message.content?.startsWith(prefix)) {
// Split the message into an array of arguments.
// Example: !ping hello world
// Output: ["ping", "hello", "world"]
const args = message.content.slice(prefix.length).split(/ +/);
// Shift the first argument from the array.
// Example: ["ping", "hello", "world"]
// Output: "ping"
const command = args.shift()?.toLowerCase();
switch (command) {
case "ping":
// message.Reply will reply to the user who sent the message.
// Example: message.Reply("Pong!");
// Output: Replying to <username>: Pong!
message.Reply("Pong!");
break;
case "prefix":
prefix = args[0];
message.Reply("Prefix set to " + prefix);
break;
case "say":
// message.Send will send a message to the chat.
// Example: message.Send("Hello, world!");
// Output: Hello, world!
// Note: This will not reply to the user.
message.Send(args.join(" "));
break;
case "help":
message.Reply("Commands: help, ping, say, prefix");
break;
}
}
});
// Connects to the Websocket server.
client.Connect();