{"size": 1838, "ext": "js", "max_stars_repo_path": "functions/CMD_hub_leave.js", "max_stars_repo_name": "FlipperLP/I-SH", "max_stars_repo_stars_event_min_datetime": "2022-02-28T17:34:17.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-28T17:34:17.000Z", "max_issues_repo_path": "functions/CMD_hub_leave.js", "max_issues_repo_name": "FlipperLP/I-SH", "max_issues_repo_issues_event_min_datetime": "2021-08-16T19:14:26.000Z", "max_issues_repo_issues_event_max_datetime": "2021-08-16T19:14:26.000Z", "max_forks_repo_path": "functions/CMD_hub_leave.js", "max_forks_repo_name": "FlipperLP/I-SH", "max_forks_repo_forks_event_min_datetime": "2022-02-28T17:51:52.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-28T17:51:52.000Z"}
{"max_stars_count": 1, "max_issues_count": 1, "max_forks_count": 1, "avg_line_length": 39.1063829787, "max_line_length": 178, "alphanum_fraction": 0.7350380849}
const BridgedChannel = require('../database/models/bridgedChannel');

const errHander = (err) => { console.error('ERROR:', err); };

// creates a embed messagetemplate for succeded actions
function messageSuccess(client, message, body) {
  client.functions.get('FUNC_MessageEmbedMessage')
    .run(client.user, message.channel, body, '', 4296754, false);
}

// creates a embed messagetemplate for failed actions
function messageFail(client, message, body) {
  client.functions.get('FUNC_MessageEmbedMessage')
    .run(client.user, message.channel, body, '', 16449540, false);
}

// deletes channel entry from DB
async function deleteChannel(channelID) {
  const result = await BridgedChannel.destroy({ where: { channelID } }).catch(errHander);
  return result;
}

async function checkPermissions(message) {
  const permissions = await message.client.functions.get('FUNC_checkPermissions').run(message.member, message, 'MANAGE_CHANNELS');
  return permissions;
}

module.exports.run = async (client, message, args, config) => {
  // check DM
  if (message.channel.type === 'dm') return messageFail(client, message, 'You can\'t unlink a DM channel!');
  // check user permissions
  if (!await checkPermissions(message)) return messageFail(client, message, 'Hold on! You dont have permissions to manage this channel. Try asking an admin to remove the link.');

  // get custom channel id
  const channelID = message.channel.id;
  // delete channel entry from database
  const created = await deleteChannel(channelID);
  // check if channel entry is deleted and give feedback
  if (created) messageSuccess(client, message, 'This channel is now not longer linked!');
  else messageFail(client, message, 'This channel is not linked with any hub! You can\'t unlink it.');
};

module.exports.help = {
  name: 'CMD_hub_leave',
  parent: 'hub',
};
