1
0
Fork 0
cloudservices/src/commands/systemd_restart.ts

31 lines
1.4 KiB
TypeScript

import { Message } from 'discord.js';
import { Client, Command } from '../class';
export default class SystemD_Restart extends Command {
constructor(client: Client) {
super(client);
this.name = 'restart';
this.description = `Restarts a SystemD user service, if the service has never been started or isn't running use \`${this.client.config.prefix}systemd start <service name>\`.`;
this.usage = `${this.client.config.prefix}systemd restart <service name>`;
this.enabled = false;
}
public async run(message: Message, args: string[]) {
try {
if (!args[0]) return this.client.commands.get('help').run(message, ['systemd', this.name]);
const account = await this.client.db.Account.findOne({ userId: message.author.id });
if (!account) return this.error(message.channel, 'You do not have a Cloud Services account.');
try {
await this.client.util.exec(`runuser ${account.username} -c 'DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/\${UID}/bus" systemctl --user restart ${args[0]}'`);
message.delete();
return this.success(message.channel, 'Service restarted.');
} catch (err) {
if (err.toString().includes('could not be found')) return this.error(message.channel, 'Service not found.');
return this.error(message.channel, err.toString());
}
} catch (err) {
return this.client.util.handleError(err, message, this);
}
}
}