Skip to content

Instantly share code, notes, and snippets.

@zero734kr
Created August 17, 2020 22:49
Show Gist options
  • Save zero734kr/73b9a7d7cbc13c1b62c1c27d7001ccad to your computer and use it in GitHub Desktop.
Save zero734kr/73b9a7d7cbc13c1b62c1c27d7001ccad to your computer and use it in GitHub Desktop.
const Command = require("../utils/command")
class Limit extends Command {
constructor(client) {
super(client)
this.name = "채팅제한"
this.aliases = ["coxldwpgks"]
this.category = "관리"
this.description = "도배를 막기 위해 명령어 입력으로 특정 채널에 줄 바꿈 채팅 제한을 거는 기능"
this.usage = "채팅제한 <활성화/비활성화/목록>\n채팅제한 추가 <...줄> [<채널> <채널> <채널> ...]\n채팅제한 삭제 [<채널> <채널> <채널> ...]"
this.perm = 3
this.dir = __filename
}
async run(message, args) {
let channel = args[0]
const query = args[1]
if (channel === "활성화") {
return this.client.database.models.get("bot").findByIdAndUpdate("bot", { $set: { disableMessageNewlineLimit: false } })
.then(() => message.channel.send(`${message.author} 님, 성공적으로 모든 채널들의 줄 제한이 활성화 되었습니다.`))
.catch(err => message.channel.send(err))
} else if (channel === "비활성화") {
return this.client.database.models.get("bot").findByIdAndUpdate("bot", { $set: { disableMessageNewlineLimit: true } })
.then(() => message.channel.send(`${message.author} 님, 성공적으로 모든 채널들의 줄 제한이 비활성화 되었습니다.`))
.catch(err => message.channel.send(err))
} else if (channel === "추가") {
const lines = parseInt(args[2], 10)
const channels = args.slice(2).map(f => message.guild.channels.resolve(f.replace(/[^0-9]/gi, "")))
if (isNaN(lines)) return message.channel.send(`${message.author} 님, \`\`${this.usage}\`\``)
if (!channels || channels.length <= 0) return message.channel.send(`${message.author} 님, \`\`${this.usage}\`\``)
const docs = await this.client.database.models.get("channel").find({ _id: channels.map(x => x.id) })
if (!docs || docs.length <= 0) return message.channel.send(`${message.author} 님, 해당 채널들을 찾을수 없습니다.`)
const updated = await this.client.database.models.get("channel").updateMany({
_id: channels.map(x => x.id)
}, {
$set: {
block: lines, disabled: false
}
}, { upsert: true })
return message.channel.send(`${message.author} 님, ${channels[0].toString()} 외 ${updated.ok - 1}개의 채널들이 업데이트 되었습니다.`)
} else if (channel === "삭제") {
const channels = args.slice(1).map(f => message.guild.channels.resolve(f.replace(/[^0-9]/gi, "")))
if (!channels || channels.length <= 0) return message.channel.send(`${message.author} 님, \`\`${this.usage}\`\``)
const docs = await this.client.database.models.get("channel").find({ _id: channels.map(x => x.id) })
if (!docs || docs.length <= 0) return message.channel.send(`${message.author} 님, 해당 채널들을 찾을수 없습니다.`)
const updated = await this.client.database.models.get("channel").updateMany({
_id: channels.map(x => x.id)
}, {
$set: {
disabled: true
}
}, { upsert: true })
return message.channel.send(`${message.author} 님, ${channels[0].toString()} 외 ${updated.ok - 1}개의 채널들이 업데이트 되었습니다.`)
} else if (channel === "목록") {
if(query === "초기화") {
const msg = await message.channel.send(`${message.author} 님, 목록을 초기화 하는 중입니다...`)
await require("mongoose").connection.dropCollection("channels").then(() => msg.edit(`${message.author} 님, 채팅제한의 저장소가 삭제 되었습니다. 재생성 중...`))
await require("mongoose").connection.createCollection("channels")
return msg.edit(`${message.author} 님, 채팅제한의 저장소가 성공적으로 초기화 되었습니다.`)
}
const msg = await message.channel.send(`${message.author} 님, 목록을 불러오는 중입니다...`)
const docs = await this.client.database.models.get("channel").find({ disabled: true })
const text = docs.map(x => `<#${x._id}> - ${x.block}줄`).join("\n")
const gen = require("hastebin-gen").default || require("hastebin-gen")
let embed = new this.embed()
.setTitle("채팅제한 목록")
.setTimestamp()
.setFooter(message.author.username, message.author.avatarURL())
.setColor(this.client.color)
.setDescription(text.length > 2000 ? gen(text, "txt") : text)
return msg.edit("", { embed })
}
channel = message.guild.channels.resolve(channel)
if (!channel) return message.channel.send(`${message.author} 님, \`\`${this.usage}\`\``)
if (!query) return message.channel.send(`${message.author} 님, \`\`${this.usage}\`\``)
if (query === "활성화") {
const doc = await this.client.database.models.get("channel").findById(channel.id)
if (!doc) return message.channel.send(`${message.author} 님, 해당 채널의 메세지 줄 제한이 존재하지 않아 활성화에 실패했습니다. \`\`채팅제한 ${channel.id} <숫자>\`\`를 먼저 사용해 주세요.`)
this.client.database.models.get("channel").findByIdAndUpdate(channel.id, { $set: { disabled: false } }, { upsert: true })
.then(() => message.channel.send(`${message.author} 님, 성공적으로 ${channel} 채널의 줄 제한이 활성화 되었습니다.`))
.catch(err => message.channel.send(err))
} else if (query === "비활성화") {
const doc = await this.client.database.models.get("channel").findById(channel.id)
if (!doc) return message.channel.send(`${message.author} 님, 해당 채널의 메세지 줄 제한이 존재하지 않아 비활성화에 실패했습니다. \`\`채팅제한 ${channel.id} <숫자>\`\`를 먼저 사용해 주세요.`)
this.client.database.models.get("channel").findByIdAndUpdate(channel.id, { $set: { disabled: true } }, { upsert: true })
.then(() => message.channel.send(`${message.author} 님, 성공적으로 ${channel} 채널의 줄 제한이 비활성화 되었습니다.`))
.catch(err => message.channel.send(err))
} else {
if (isNaN(parseInt(query, 10))) return message.channel.send(`${message.author} 님, \`\`${this.usage}\`\``)
const doc = await this.client.database.models.get("channel").findById(channel.id)
if (!doc) return this.client.database.models.get("channel").create({ _id: channel.id, block: query }).then(() => message.channel.send(`${message.author} 님, 성공적으로 ${channel} 채널의 ${query}줄 제한이 활성화 되었습니다.`))
.catch(err => message.channel.send(err))
this.client.database.models.get("channel").findByIdAndUpdate(channel.id, { $set: { block: query, disabled: false } })
.then(() => message.channel.send(`${message.author} 님, 성공적으로 ${channel} 채널의 ${query}줄 제한이 활성화 되었습니다.`))
.catch(err => message.channel.send(err))
}
}
}
module.exports = Limit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment