· 5 years ago · Oct 06, 2020, 08:22 PM
1// Welcome to https://pylon.bot! You can use this editor to write custom code that Pylon will execute!
2// Be sure to check out the documentation here: https://pylon.bot/docs/reference/index.html
3// For help, join our Discord server: https://discord.gg/hC6Bbtj
4//
5// This is the default script, that shows off some examples. Feel free to obliterate all this and start
6// from scratch if you know what you're doing!
7//
8// **FAQ**:
9// - Q: How do I publish my script
10// A: Just press Ctrl + S, and it's published instantly.
11// - Q: What's that black box at the bottom of the editor do?
12// A: That's the console, you can log stuff to it, to help you develop your scripts, just use `console.log()`.
13// Additionally, if your script throws an error, it'll be logged there in real time!
14// - Q: So, I can basically write any code that I want here?
15// A: Yup! Pylon provides an isolated sandbox that runs your script safely. There are some memory and
16// execution time limits you'll have to be aware of. Check out: https://pylon.bot/docs/dev-limits
17
18// Here's an example of how to use the built in command handler.
19const commands = new discord.command.CommandGroup({
20 defaultPrefix: '/' // You can customize your default prefix here.
21});
22
23discord.on('MESSAGE_CREATE', async (message) => {
24 if (message.content.toLowerCase().includes('hi ')) {
25 await message.reply('added 2 honnor points to your account');
26 }
27});
28
29discord.on('MESSAGE_CREATE', async (message2) => {
30 if (message2.content.toLowerCase().includes('hello ')) {
31 await message2.reply('added 2 honnor points to your account');
32 }
33});
34
35discord.on('MESSAGE_CREATE', async (message3) => {
36 if (message3.content.toLowerCase().includes('thanks ')) {
37 await message3.reply('added 2 honnor points to your account');
38 }
39});
40
41discord.on('MESSAGE_CREATE', async (message4) => {
42 if (message4.content.toLowerCase().includes('thanks ')) {
43 await message4.reply('added 2 honnor points to your account');
44 }
45});
46
47commands.registerCommand(
48 'Help',
49 (args) => ({}),
50 async ({ message }, {}) => {
51 // build the rich embed
52 const richEmbed = new discord.Embed();
53 richEmbed.setTitle('Help').setColor(0x00ff00);
54 richEmbed.setDescription('Availible Commands for this server');
55 richEmbed.addField({
56 name: '[Server_help v. 7.2.3]',
57 value:
58 '___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ',
59 inline: false
60 });
61 richEmbed.addField({
62 name: '/help',
63 value: 'Lists the availible commands on the server.',
64 inline: false
65 });
66 richEmbed.addField({
67 name: '/userinfo <user>',
68 value: 'Prints Additional info about the user into the chat.',
69 inline: false
70 });
71 richEmbed.addField({
72 name: '/ping',
73 value: "Prints 'pong', used to test the server's bot.",
74 inline: false
75 });
76 richEmbed.addField({
77 name: '/tag (set,delete,add, ) <name> <value>',
78 value: 'used to store, remove, and print strings of text.',
79 inline: false
80 });
81 richEmbed.addField({
82 name: '/kick <name> <Reason>',
83 value: 'Pretends to kickk a user.',
84 inline: false
85 });
86
87 richEmbed.setTimestamp(new Date().toISOString());
88 // reply to the command with our embed
89 await message.reply({ content: '', embed: richEmbed });
90 }
91);
92
93commands.on(
94 'hp:give',
95 (args) => ({
96 name: args.string(),
97 ammount: args.text()
98 }),
99 async (message, { name, ammount }) => {
100 await message.reply({
101 content: ` ${ammount} Honor Points were given to ${name}`,
102 allowedMentions: {
103 users: [message.author.id]
104 }
105 });
106 }
107);
108
109commands.raw('test', async (message) => {
110 // Get the author of the message
111 const user = message.author;
112
113 // Fetch the guild this message was sent in
114 const guild = await message.getGuild();
115
116 // Get the channel the message was sent in, note the 'await' keyword
117 const channel = await message.getChannel();
118
119 // Fetch role data from the guild for all the roles the user has assigned
120 const roles = await Promise.all(
121 message.member.roles.map((roleId) => guild.getRole(roleId))
122 );
123
124 // Construct a list of role names, separated by new lines
125 const roleNames = roles
126 .map((role) => {
127 if (!role) return;
128 return ` - ${role.name}`;
129 })
130 .join('\n');
131
132 // Reply with some data we found
133 await message.reply(
134 `You are ${user.toMention()} sending a message in ${channel.toMention()}. You have the following roles:\n${roleNames}`
135 );
136});
137
138commands.registerCommand(
139 'userinfo',
140 (args) => ({
141 user: args.user()
142 }),
143 async ({ message }, { user }) => {
144 // build the rich embed
145 const richEmbed = new discord.Embed();
146 richEmbed.setTitle(user.getTag()).setColor(0x00ff00);
147 richEmbed.setDescription('User Information');
148 richEmbed.setThumbnail({ url: user.getAvatarUrl() });
149 richEmbed.addField({
150 name: 'User ID',
151 value: user.id,
152 inline: false
153 });
154 richEmbed.setTimestamp(new Date().toISOString());
155 // reply to the command with our embed
156 await message.reply({ content: '', embed: richEmbed });
157 }
158);
159
160// A simple command, !ping -> Pong!
161commands.raw('ping', (message) => message.reply('pong!'));
162
163commands.raw('info', (message) =>
164 message.reply('```Regicide Army Discord Server Current Ping [31ms]```')
165);
166
167// Another example, fetching some data from the web, using the `fetch` function.
168commands.raw('catfact', async (message) => {
169 // We're going to fetch a fact from the cat-fact API.
170 const req = await fetch('https://catfact.ninja/fact');
171 // Parse the request's JSON body:
172 const data = await req.json();
173 // Reply to the command with the random cat fact. It's very important we call `await` here,
174 // as this will keep the command handler running until the message is sent. You can also reply
175 // with rich embeds, to make your message replies look awesome!
176 // await message.reply(
177 // new discord.Embed({
178 // title: '? A random cat fact ?',
179 // color: 0x00ff00,
180 // description: data['fact'],
181 // footer: {
182 // text: 'powered by https://catfact.ninja'
183 // }
184 // })
185 // );
186 //});
187
188 // Example of console logging the message:
189 commands.raw('log', async (message) => {
190 console.log(message);
191 });
192
193 // What about commands that have arguments? Let's build a kick command that will kick a user and
194 // specify a reason in chat.
195 commands.on(
196 // You can also customize your command a bit further, for example, you can limit who can call the
197 // command by using filters. In this case, we only want the kick command to be usable
198 // by server admins.
199 // For more, see: https://pylon.bot/docs/reference/modules/discord.command.filters.html
200 { name: 'kick', filters: discord.command.filters.isAdministrator() },
201 // You can specify your arguments here like this. There are many argument types
202 // to pick from, see: https://pylon.bot/docs/reference/interfaces/discord.command.icommandargs.html
203 (ctx) => ({
204 member: ctx.guildMember(),
205 reason: ctx.textOptional()
206 }),
207 // This function will then be called when the command is issued. It will have the message,
208 // as the first argument, and the arguments you've specified as the second argument.
209 // For more, see: https://pylon.bot/docs/reference/classes/discord.command.commandgroup.html#on
210 async (message, { member, reason }) => {
211 await message.reply(
212 `Cya, ${member.toMention()} - you're kicked because ${reason}!`
213 );
214 // TODO: uncomment this to actually have the bot kick someone.
215 // await member.kick({reason});
216 await message.reply(`JK!`);
217 }
218 );
219
220 // Here's an example of sub-commands. We'll do a quick and easy "tag" system, leveraging
221 // the Pylon Key Value store, which we'll use as a database to store tags to their values.
222 // For more on the KV Store, check out: https://pylon.bot/docs/reference/classes/pylon.kvnamespace.html
223
224 // A key value store is per server. Let's create one now, and call it "tags".
225 const tagsKv = new pylon.KVNamespace('tags');
226
227 // Let's now register a `"tag"` command. The thought is we should be able to do:
228 // !tag set <key: string> <value: text> - In order to set a tag.
229 // !tag delete <key: string> - In order to delete a tag. Only usable by people who can manage messages.
230 // !tag <key: string> - In order to retrieve a tag.
231 commands.subcommand('tag', (subcommand) => {
232 subcommand.on(
233 'set',
234 (ctx) => ({
235 // At this point you may be wondering what the difference between "string" and "text" is.
236 // Basically, "string" captures an argument delimited by spaces, and "text" will capture the rest
237 // of the message. So our usage of `!tag set pylon Pylon is a pretty cool bot`, will capture,
238 // "pylon" as the key, and "Pylon is a pretty cool bot" as the value.
239 key: ctx.string(),
240 value: ctx.text()
241 }),
242 async (message, { key, value }) => {
243 // Save the tag to the database.
244 await tagsKv.put(key, value);
245 // Reply to the user to say we've saved their tag. We're also using the `allowedMentions` feature here,
246 // to make sure thet the message won't mention anyone, for example, if they made the tag key "@everyone",
247 // it wouldn't ping everyone with `allowedMentions: {}`.
248 await message.reply({
249 content: `Alright, I've saved the tag for **${key}**!`,
250 allowedMentions: {}
251 });
252 }
253 );
254
255 subcommand.on(
256 // This one is pretty self explanatory, once you've got the hang of commands. Here we're building a command
257 // that is only usable by people with the MANGE_MESSAGES permission.
258 { name: 'delete', filters: discord.command.filters.canManageMessages() },
259 (ctx) => ({ key: ctx.string() }),
260 async (message, { key }) => {
261 // Delete the tag from the database.
262 await tagsKv.delete(key);
263 // Reply with a confirmation message.
264 await message.reply({
265 content: `Alright, I've deleted the tag for **${key}**!`,
266 allowedMentions: {}
267 });
268 }
269 );
270
271 // And finally, let's have a default command handler, for if the command was neither "set" or "delete".
272 subcommand.default(
273 (ctx) => ({ key: ctx.string() }),
274 async (message, { key }) => {
275 // Retrieve the tag from the database. We are using `.get<string>(...)` as we have stored a string
276 // in the database before.
277 const value = await tagsKv.get<string>(key);
278
279 if (value == null) {
280 // If the value is null, that means that the tag did not exist in the database.
281 await message.reply({
282 content: `Unknown tag: **${key}**`,
283 allowedMentions: {}
284 });
285 } else {
286 // Otherwise, let's send back the tag value. Again, we're using `allowedMentions: {}` here
287 // to ensure that the bot sending the tag is not able to ping anyone!
288 await message.reply({ content: value, allowedMentions: {} });
289 }
290 }
291 );
292 });
293
294 // Finally, an example of "raw" message handling, without using the command handler.
295 // Here, we're checking to see if the message contains the string "ayy", and if it does,
296 // we'll reply with "lmao".
297 discord.on('MESSAGE_CREATE', async (message) => {
298 // There are many, many more events you can listen for with Pylon. For more, check out:
299 // https://pylon.bot/docs/reference/modules/discord.html#on
300 // This marks the end of the default script. From here, you can customize Pylon however you want.
301 // The possibilities are literally endless. If you encounter bugs, need help, or want to share what
302 // you've built. Join the Pylon discord. The invite link is here: https://discord.gg/hC6Bbtj
303 });
304 const command = new discord.command.CommandGroup({
305 additionalPrefixes: '/HC ' // You can customize your default prefix here.
306 });
307
308 command.raw('setup', (message) => message.reply('> ```setting up```'));
309
310 command.raw('honorprinter', (message) =>
311 message.reply(
312 '> ```HonorPrinter set up, printing 20,000 HonorPoints a minute```'
313 )
314 );
315
316 command.raw('offlinemode', (message) =>
317 message.reply('> ```HonorControl is going offline mode for 24 hours```')
318 );
319});
320