Skip to content

Instantly share code, notes, and snippets.

@JacobTyrrell
Created May 27, 2020 22:45
Show Gist options
  • Save JacobTyrrell/97b5fb2a7176e01c8b0d0f254c612128 to your computer and use it in GitHub Desktop.
Save JacobTyrrell/97b5fb2a7176e01c8b0d0f254c612128 to your computer and use it in GitHub Desktop.
extension Shield {
func addMuteCmd() {
self.register("mute", with: muteMeta) { msg, args in
// Function
var em = Embed.init()
em.title = "Mute"
// Check that the caller has the appropriate permissions
guard let member = msg.member, member.hasPermission(.manageMessages) else {
em.description = "Sorry, you don't have the required permission: `MANAGE_ROLES`";
return
}
// Make sure there was a highlight and fetch the appropriate member
guard !args.isEmpty, let targetUser = msg.mentions.first else {
em.description = "Usage: .mute <@user> <reason>";
msg.reply(with: em);
return
}
// Drop the mention from the args
let newArgs = args.dropFirst()
// Get the reason message ready to go
let reason = newArgs.joined(separator: " ")
// Get the guild that the message came from
guard let guild = client.getGuild(for: msg.channel.id), let targetMember = guild.members[targetUser.id] else {
return
}
// Make sure there is a role called "muted"
guard let mutedRole = guild.roles.values.first(where: {
role in role.name.lowercased() == "muted"
}) else {
em.description = "Sorry, I couldn't find a role named `muted`. Please create the required role and try again!";
msg.reply(with: em);
return
}
// // Duplicate a new array and append the mutedRole
// var targetRoleIds = targetMember.roles.map { $0.id.rawValue }
// targetRoleIds.append(mutedRole.id.rawValue)
// print(targetRoleIds)
//
// // Apply the role "muted" to the targetUser:
// guild.modifyMember(targetUser.id, with: ["roles": targetRoleIds])
guild.modifyMember(targetUser.id, with: ["roles": targetMember.roles.map { $0.id.rawValue } + [mutedRole.id.rawValue]) { error in
guard error == nil else { em.description = "\(error!)"; msg.reply(with: em); return }
em.description = "Successfully muted <@\(targetUser.id)> because: \(reason)"
msg.reply(with: em)
}
em.description = "Successfully muted <@\(targetUser.id)> because: \(reason)"
msg.reply(with: em)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment