/ src / modules / commands / export.ts
export.ts
 1  import { SlashCommandBuilder } from 'discord.js';
 2  import fs from 'fs';
 3  
 4  import { prepare, DBActivityRole, getLang } from '../db';
 5  import { Command } from '../commandHandler';
 6  import { __, __h_dc } from '../messages';
 7  
 8  export default {
 9    data: new SlashCommandBuilder()
10      .setName('export')
11      .setDescription('Exports all activity roles in your guild.')
12      .setDescriptionLocalizations(__h_dc('Lists all activity roles in your guild.'))
13      .setDMPermission(false)
14      .addBooleanOption(option =>
15        option
16          .setName('roleids')
17          .setDescription('Show role IDs (can’t be imported in another guild)')
18          .setDescriptionLocalizations(__h_dc('Show role IDs (can’t be imported in another guild)'))
19          .setRequired(false)
20      ),
21  
22    execute: async interaction => {
23      const locale = getLang(interaction);
24      const roleIDs = (interaction.options.get('roleids') as boolean | null) ?? false;
25  
26      const activityRoles: DBActivityRole[] =
27        prepare('SELECT * FROM activityRoles WHERE guildID = ?')
28          .all(interaction.guild!.id) as any as DBActivityRole[];
29      if (activityRoles.length === 0) {
30        interaction.reply({
31          content: __({ phrase: 'There are no activity roles in this guild.', locale })
32        });
33        return;
34      }
35  
36      /*
37      Import is: ActivityName,ExactActivityNaime,Permanent,RoleName,RoleColor, so we can import on another server
38      */
39      let output = '# ActivityName,\tmode,\tpermanent,\tRoleName,\tRoleColor';
40      if (roleIDs) output += '\t# RoleID';
41      output += '\n\n';
42  
43      for (const activityRole of activityRoles) {
44        const role = interaction.guild!.roles.cache.find(role => role.id === activityRole.roleID);
45        output +=
46          activityRole.activityName +
47          `,\t${activityRole.exactActivityName}` +
48          `,\t${Number(!activityRole.live)}` +
49          `,\t\t${role?.name}` +
50          `,\t${role?.hexColor}`;
51        if (roleIDs) output += `  \t# ${activityRole.roleID}`;
52        output += '\n';
53      }
54  
55      const filename = `export-${interaction.id.substring(0, 7)}.txt`;
56      fs.writeFileSync(filename, output);
57      await interaction.reply({
58        files: [filename]
59      });
60      fs.unlinkSync(filename);
61    }
62  } as Command;