commands.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. "runtime"
  9. "strings"
  10. "time"
  11. "github.com/Necroforger/dgrouter/exrouter"
  12. "github.com/bwmarrin/discordgo"
  13. "github.com/fatih/color"
  14. "github.com/hako/durafmt"
  15. "github.com/kennygrant/sanitize"
  16. )
  17. // Multiple use messages to save space and make cleaner.
  18. // TODO: Implement this for more?
  19. const (
  20. cmderrLackingBotAdminPerms = "You do not have permission to use this command. Your User ID must be set as a bot administrator in the settings file."
  21. cmderrSendFailure = "Failed to send command message (requested by %s)...\t%s"
  22. )
  23. func handleCommands() *exrouter.Route {
  24. router := exrouter.New()
  25. //#region Utility Commands
  26. go router.On("ping", func(ctx *exrouter.Context) {
  27. if isCommandableChannel(ctx.Msg) {
  28. if !hasPerms(ctx.Msg.ChannelID, discordgo.PermissionSendMessages) {
  29. log.Println(lg("Command", "Ping", color.HiRedString, fmtBotSendPerm, ctx.Msg.ChannelID))
  30. } else {
  31. beforePong := time.Now()
  32. pong, err := ctx.Reply("Pong!")
  33. if err != nil {
  34. log.Println(lg("Command", "Ping", color.HiRedString, "Error sending pong message:\t%s", err))
  35. } else {
  36. afterPong := time.Now()
  37. latency := bot.HeartbeatLatency().Milliseconds()
  38. roundtrip := afterPong.Sub(beforePong).Milliseconds()
  39. mention := ctx.Msg.Author.Mention()
  40. content := fmt.Sprintf("**Latency:** ``%dms`` — **Roundtrip:** ``%dms``",
  41. latency,
  42. roundtrip,
  43. )
  44. if pong != nil {
  45. if selfbot {
  46. bot.ChannelMessageEdit(pong.ChannelID, pong.ID, fmt.Sprintf("%s **Command — Ping**\n\n%s", mention, content))
  47. } else {
  48. bot.ChannelMessageEditComplex(&discordgo.MessageEdit{
  49. ID: pong.ID,
  50. Channel: pong.ChannelID,
  51. Content: &mention,
  52. Embed: buildEmbed(ctx.Msg.ChannelID, "Command — Ping", content),
  53. })
  54. }
  55. }
  56. // Log
  57. log.Println(lg("Command", "Ping", color.HiCyanString, "%s pinged bot - Latency: %dms, Roundtrip: %dms",
  58. getUserIdentifier(*ctx.Msg.Author),
  59. latency,
  60. roundtrip),
  61. )
  62. }
  63. }
  64. }
  65. }).Cat("Utility").Alias("test").Desc("Pings the bot")
  66. go router.On("help", func(ctx *exrouter.Context) {
  67. if isCommandableChannel(ctx.Msg) {
  68. if !hasPerms(ctx.Msg.ChannelID, discordgo.PermissionSendMessages) {
  69. log.Println(lg("Command", "Help", color.HiRedString, fmtBotSendPerm, ctx.Msg.ChannelID))
  70. } else {
  71. content := ""
  72. for _, cmd := range router.Routes {
  73. if cmd.Category != "Admin" || isBotAdmin(ctx.Msg) {
  74. content += fmt.Sprintf("• \"%s\" : %s",
  75. cmd.Name,
  76. cmd.Description,
  77. )
  78. if len(cmd.Aliases) > 0 {
  79. content += fmt.Sprintf("\n— Aliases: \"%s\"", strings.Join(cmd.Aliases, "\", \""))
  80. }
  81. content += "\n\n"
  82. }
  83. }
  84. if _, err := replyEmbed(ctx.Msg, "Command — Help",
  85. fmt.Sprintf("Use commands as ``\"%s<command> <arguments?>\"``\n```%s```\n%s",
  86. config.CommandPrefix, content, projectRepoURL)); err != nil {
  87. log.Println(lg("Command", "Help", color.HiRedString, cmderrSendFailure, getUserIdentifier(*ctx.Msg.Author), err))
  88. }
  89. log.Println(lg("Command", "Help", color.HiCyanString, "%s asked for help", getUserIdentifier(*ctx.Msg.Author)))
  90. }
  91. }
  92. }).Cat("Utility").Alias("commands").Desc("Outputs this help menu")
  93. //#endregion
  94. //#region Info Commands
  95. go router.On("status", func(ctx *exrouter.Context) {
  96. if isCommandableChannel(ctx.Msg) {
  97. if !hasPerms(ctx.Msg.ChannelID, discordgo.PermissionSendMessages) {
  98. log.Println(lg("Command", "Status", color.HiRedString, fmtBotSendPerm, ctx.Msg.ChannelID))
  99. } else {
  100. message := fmt.Sprintf("• **Uptime —** %s\n"+
  101. "• **Started at —** %s\n"+
  102. "• **Joined Servers —** %d\n"+
  103. "• **Bound Channels —** %d\n"+
  104. "• **Bound Cagetories —** %d\n"+
  105. "• **Bound Servers —** %d\n"+
  106. "• **Bound Users —** %d\n"+
  107. "• **Admin Channels —** %d\n"+
  108. "• **Heartbeat Latency —** %dms",
  109. durafmt.Parse(time.Since(startTime)).String(),
  110. startTime.Format("03:04:05pm on Monday, January 2, 2006 (MST)"),
  111. len(bot.State.Guilds),
  112. getBoundChannelsCount(),
  113. getBoundCategoriesCount(),
  114. getBoundServersCount(),
  115. getBoundUsersCount(),
  116. len(config.AdminChannels),
  117. bot.HeartbeatLatency().Milliseconds(),
  118. )
  119. if sourceConfig := getSource(ctx.Msg); sourceConfig != emptyConfig {
  120. configJson, _ := json.MarshalIndent(sourceConfig, "", "\t")
  121. message = message + fmt.Sprintf("\n• **Channel Settings...** ```%s```", string(configJson))
  122. }
  123. if _, err := replyEmbed(ctx.Msg, "Command — Status", message); err != nil {
  124. log.Println(lg("Command", "Status", color.HiRedString, cmderrSendFailure, getUserIdentifier(*ctx.Msg.Author), err))
  125. }
  126. log.Println(lg("Command", "Status", color.HiCyanString, "%s requested status report", getUserIdentifier(*ctx.Msg.Author)))
  127. }
  128. }
  129. }).Cat("Info").Desc("Displays info regarding the current status of the bot")
  130. go router.On("stats", func(ctx *exrouter.Context) {
  131. if isCommandableChannel(ctx.Msg) {
  132. if !hasPerms(ctx.Msg.ChannelID, discordgo.PermissionSendMessages) {
  133. log.Println(lg("Command", "Stats", color.HiRedString, fmtBotSendPerm, ctx.Msg.ChannelID))
  134. } else {
  135. if sourceConfig := getSource(ctx.Msg); sourceConfig != emptyConfig {
  136. if *sourceConfig.AllowCommands {
  137. content := fmt.Sprintf("• **Total Downloads —** %s\n"+
  138. "• **Downloads in this Channel —** %s",
  139. formatNumber(int64(dbDownloadCount())),
  140. formatNumber(int64(dbDownloadCountByChannel(ctx.Msg.ChannelID))),
  141. )
  142. //TODO: Count in channel by users
  143. if _, err := replyEmbed(ctx.Msg, "Command — Stats", content); err != nil {
  144. log.Println(lg("Command", "Stats", color.HiRedString, cmderrSendFailure,
  145. getUserIdentifier(*ctx.Msg.Author), err))
  146. }
  147. log.Println(lg("Command", "Stats", color.HiCyanString, "%s requested stats",
  148. getUserIdentifier(*ctx.Msg.Author)))
  149. }
  150. }
  151. }
  152. }
  153. }).Cat("Info").Desc("Outputs statistics regarding this channel")
  154. go router.On("info", func(ctx *exrouter.Context) {
  155. if isCommandableChannel(ctx.Msg) {
  156. if !hasPerms(ctx.Msg.ChannelID, discordgo.PermissionSendMessages) {
  157. log.Println(lg("Command", "Info", color.HiRedString, fmtBotSendPerm, ctx.Msg.ChannelID))
  158. } else {
  159. content := fmt.Sprintf("Here is some useful info...\n\n"+
  160. "• **Your User ID —** `%s`\n"+
  161. "• **Bots User ID —** `%s`\n"+
  162. "• **This Channel ID —** `%s`\n"+
  163. "• **This Server ID —** `%s`\n\n"+
  164. "• **Versions —`%s, discordgo v%s (modified), Discord API v%s`"+
  165. "\n\nRemember to remove any spaces when copying to settings.",
  166. ctx.Msg.Author.ID, botUser.ID, ctx.Msg.ChannelID, ctx.Msg.GuildID, runtime.Version(), discordgo.VERSION, discordgo.APIVersion)
  167. if _, err := replyEmbed(ctx.Msg, "Command — Info", content); err != nil {
  168. log.Println(lg("Command", "Info", color.HiRedString, cmderrSendFailure, getUserIdentifier(*ctx.Msg.Author), err))
  169. }
  170. log.Println(lg("Command", "Info", color.HiCyanString, "%s requested info", getUserIdentifier(*ctx.Msg.Author)))
  171. }
  172. }
  173. }).Cat("Info").Alias("debug").Desc("Displays info regarding Discord IDs")
  174. //#endregion
  175. //#region Admin Commands
  176. go router.On("history", func(ctx *exrouter.Context) {
  177. if isCommandableChannel(ctx.Msg) {
  178. // Vars
  179. var channels []string
  180. var shouldAbort bool = false
  181. var shouldProcess bool = true
  182. var before string
  183. var beforeID string
  184. var since string
  185. var sinceID string
  186. //#region Parse Args
  187. for argKey, argValue := range ctx.Args {
  188. if argKey == 0 { // skip head
  189. continue
  190. }
  191. if strings.Contains(strings.ToLower(argValue), "cancel") ||
  192. strings.Contains(strings.ToLower(argValue), "stop") {
  193. shouldAbort = true
  194. } else if strings.Contains(strings.ToLower(argValue), "help") ||
  195. strings.Contains(strings.ToLower(argValue), "info") {
  196. shouldProcess = false
  197. if hasPerms(ctx.Msg.ChannelID, discordgo.PermissionSendMessages) {
  198. //content := fmt.Sprintf("")
  199. _, err := replyEmbed(ctx.Msg, "Command — History Help", "TODO: this")
  200. if err != nil {
  201. log.Println(lg("Command", "History",
  202. color.HiRedString, cmderrSendFailure, getUserIdentifier(*ctx.Msg.Author), err))
  203. }
  204. } else {
  205. log.Println(lg("Command", "History", color.HiRedString, fmtBotSendPerm, ctx.Msg.ChannelID))
  206. }
  207. log.Println(lg("Command", "History", color.CyanString, "%s requested history help.", getUserIdentifier(*ctx.Msg.Author)))
  208. } else if strings.Contains(strings.ToLower(argValue), "list") ||
  209. strings.Contains(strings.ToLower(argValue), "status") ||
  210. strings.Contains(strings.ToLower(argValue), "output") {
  211. shouldProcess = false
  212. output := "Running history jobs...\n"
  213. for channelID, job := range historyJobs {
  214. channelLabel := channelID
  215. channelInfo, err := bot.State.Channel(channelID)
  216. if err == nil {
  217. channelLabel = "#" + channelInfo.Name
  218. }
  219. output += fmt.Sprintf("• _%s_ - (%s)`%s`, `updated %s ago, added %s ago`\n",
  220. historyStatusLabel(job.Status), job.OriginUser, channelLabel,
  221. durafmt.ParseShort(time.Since(job.Updated)).String(), durafmt.ParseShort(time.Since(job.Added)).String())
  222. log.Println(lg("Command", "History", color.HiCyanString, "History Job: %s - (%s)%s, updated %s ago, added %s ago",
  223. historyStatusLabel(job.Status), job.OriginUser, channelLabel,
  224. durafmt.ParseShort(time.Since(job.Updated)).String(), durafmt.ParseShort(time.Since(job.Added)).String()))
  225. }
  226. if hasPerms(ctx.Msg.ChannelID, discordgo.PermissionSendMessages) {
  227. _, err := ctx.Reply(output)
  228. if err != nil {
  229. log.Println(lg("Command", "History", color.HiRedString, cmderrSendFailure, getUserIdentifier(*ctx.Msg.Author), err))
  230. }
  231. } else {
  232. log.Println(lg("Command", "History", color.HiRedString, fmtBotSendPerm, ctx.Msg.ChannelID))
  233. }
  234. log.Println(lg("Command", "History", color.HiRedString, "%s requested statuses of history jobs.",
  235. getUserIdentifier(*ctx.Msg.Author)))
  236. } else if strings.Contains(strings.ToLower(argValue), "--before=") { // before key
  237. before = strings.ReplaceAll(strings.ToLower(argValue), "--before=", "")
  238. if isDate(before) {
  239. beforeID = discordTimestampToSnowflake("2006-01-02", dateLocalToUTC(before))
  240. } else if isNumeric(before) {
  241. beforeID = before
  242. }
  243. if config.DebugOutput {
  244. log.Println(lg("Command", "History", color.CyanString, "Date before range applied, snowflake %s, converts back to %s",
  245. beforeID, discordSnowflakeToTimestamp(beforeID, "2006-01-02T15:04:05.000Z07:00")))
  246. }
  247. } else if strings.Contains(strings.ToLower(argValue), "--since=") { // since key
  248. since = strings.ReplaceAll(strings.ToLower(argValue), "--since=", "")
  249. if isDate(since) {
  250. sinceID = discordTimestampToSnowflake("2006-01-02", dateLocalToUTC(since))
  251. } else if isNumeric(since) {
  252. sinceID = since
  253. }
  254. if config.DebugOutput {
  255. log.Println(lg("Command", "History", color.CyanString, "Date since range applied, snowflake %s, converts back to %s",
  256. sinceID, discordSnowflakeToTimestamp(sinceID, "2006-01-02T15:04:05.000Z07:00")))
  257. }
  258. } else {
  259. // Actual Source ID(s)
  260. targets := strings.Split(ctx.Args.Get(argKey), ",")
  261. for _, target := range targets {
  262. if isNumeric(target) {
  263. // Test/Use if number is guild
  264. guild, err := bot.State.Guild(target)
  265. if err == nil {
  266. if config.DebugOutput {
  267. log.Println(lg("Command", "History", color.YellowString,
  268. "Specified target %s is a guild: \"%s\", adding all channels...",
  269. target, guild.Name))
  270. }
  271. for _, ch := range guild.Channels {
  272. channels = append(channels, ch.ID)
  273. if config.DebugOutput {
  274. log.Println(lg("Command", "History", color.YellowString,
  275. "Added %s (#%s in \"%s\") to history queue",
  276. ch.ID, ch.Name, guild.Name))
  277. }
  278. }
  279. } else { // Test/Use if number is channel
  280. ch, err := bot.State.Channel(target)
  281. if err == nil {
  282. channels = append(channels, target)
  283. if config.DebugOutput {
  284. log.Println(lg("Command", "History", color.YellowString, "Added %s (#%s in %s) to history queue",
  285. ch.ID, ch.Name, ch.GuildID))
  286. }
  287. }
  288. }
  289. } else if strings.Contains(strings.ToLower(target), "all") {
  290. channels = getAllRegisteredChannels()
  291. }
  292. }
  293. }
  294. }
  295. //#endregion
  296. //#region Process Channels
  297. if shouldProcess {
  298. // Local
  299. if len(channels) == 0 {
  300. channels = append(channels, ctx.Msg.ChannelID)
  301. }
  302. // Foreach Channel
  303. for _, channel := range channels {
  304. if config.DebugOutput {
  305. nameGuild := channel
  306. if chinfo, err := bot.State.Channel(channel); err == nil {
  307. nameGuild = getGuildName(chinfo.GuildID)
  308. }
  309. nameCategory := getChannelCategoryName(channel)
  310. nameChannel := getChannelName(channel)
  311. nameDisplay := fmt.Sprintf("%s / %s", nameGuild, nameChannel)
  312. if nameCategory != "unknown" {
  313. nameDisplay = fmt.Sprintf("%s / %s / %s", nameGuild, nameCategory, nameChannel)
  314. }
  315. log.Println(lg("Command", "History", color.HiMagentaString,
  316. "Queueing history job for \"%s\"\t\t(%s) ...", nameDisplay, channel))
  317. }
  318. if !isBotAdmin(ctx.Msg) {
  319. log.Println(lg("Command", "History", color.CyanString,
  320. "%s tried to cache history for %s but lacked proper permission.",
  321. getUserIdentifier(*ctx.Msg.Author), channel))
  322. if !hasPerms(ctx.Msg.ChannelID, discordgo.PermissionSendMessages) {
  323. log.Println(lg("Command", "History", color.HiRedString, fmtBotSendPerm, channel))
  324. } else {
  325. if _, err := replyEmbed(ctx.Msg, "Command — History", cmderrLackingBotAdminPerms); err != nil {
  326. log.Println(lg("Command", "History", color.HiRedString, cmderrSendFailure,
  327. getUserIdentifier(*ctx.Msg.Author), err))
  328. }
  329. }
  330. } else {
  331. // Run
  332. if !shouldAbort {
  333. if job, exists := historyJobs[channel]; !exists ||
  334. (job.Status != historyStatusDownloading && job.Status != historyStatusAbortRequested) {
  335. job.Status = historyStatusWaiting
  336. job.OriginChannel = ctx.Msg.ChannelID
  337. job.OriginUser = getUserIdentifier(*ctx.Msg.Author)
  338. job.TargetCommandingMessage = ctx.Msg
  339. job.TargetChannelID = channel
  340. job.TargetBefore = beforeID
  341. job.TargetSince = sinceID
  342. job.Updated = time.Now()
  343. job.Added = time.Now()
  344. historyJobs[channel] = job
  345. } else { // ALREADY RUNNING
  346. log.Println(lg("Command", "History", color.CyanString,
  347. "%s tried using history command but history is already running for %s...",
  348. getUserIdentifier(*ctx.Msg.Author), channel))
  349. }
  350. } else if historyJobs[channel].Status == historyStatusDownloading ||
  351. historyJobs[channel].Status == historyStatusWaiting { // requested abort while downloading
  352. if job, exists := historyJobs[channel]; exists {
  353. job.Status = historyStatusAbortRequested
  354. if historyJobs[channel].Status == historyStatusWaiting {
  355. job.Status = historyStatusAbortCompleted
  356. }
  357. historyJobs[channel] = job
  358. }
  359. log.Println(lg("Command", "History", color.CyanString,
  360. "%s cancelled history cataloging for \"%s\"",
  361. getUserIdentifier(*ctx.Msg.Author), channel))
  362. } else { // tried to stop but is not downloading
  363. log.Println(lg("Command", "History", color.CyanString,
  364. "%s tried to cancel history for \"%s\" but it's not running",
  365. getUserIdentifier(*ctx.Msg.Author), channel))
  366. }
  367. }
  368. }
  369. }
  370. //#endregion
  371. }
  372. }).Cat("Admin").Alias("catalog", "cache").Desc("Catalogs history for this channel")
  373. go router.On("exit", func(ctx *exrouter.Context) {
  374. if isCommandableChannel(ctx.Msg) {
  375. if isBotAdmin(ctx.Msg) {
  376. if !hasPerms(ctx.Msg.ChannelID, discordgo.PermissionSendMessages) {
  377. log.Println(lg("Command", "Exit", color.HiRedString, fmtBotSendPerm, ctx.Msg.ChannelID))
  378. } else {
  379. if _, err := replyEmbed(ctx.Msg, "Command — Exit", "Exiting program..."); err != nil {
  380. log.Println(lg("Command", "Exit", color.HiRedString,
  381. cmderrSendFailure, getUserIdentifier(*ctx.Msg.Author), err))
  382. }
  383. }
  384. log.Println(lg("Command", "Exit", color.HiCyanString,
  385. "%s (bot admin) requested exit, goodbye...",
  386. getUserIdentifier(*ctx.Msg.Author)))
  387. properExit()
  388. } else {
  389. if !hasPerms(ctx.Msg.ChannelID, discordgo.PermissionSendMessages) {
  390. log.Println(lg("Command", "Exit", color.HiRedString, fmtBotSendPerm, ctx.Msg.ChannelID))
  391. } else {
  392. if _, err := replyEmbed(ctx.Msg, "Command — Exit", cmderrLackingBotAdminPerms); err != nil {
  393. log.Println(lg("Command", "Exit", color.HiRedString,
  394. cmderrSendFailure, getUserIdentifier(*ctx.Msg.Author), err))
  395. }
  396. }
  397. log.Println(lg("Command", "Exit", color.HiCyanString,
  398. "%s tried to exit but lacked bot admin perms.", getUserIdentifier(*ctx.Msg.Author)))
  399. }
  400. }
  401. }).Cat("Admin").Alias("reload", "kill").Desc("Kills the bot")
  402. go router.On("emojis", func(ctx *exrouter.Context) {
  403. if isCommandableChannel(ctx.Msg) {
  404. if isBotAdmin(ctx.Msg) {
  405. if hasPerms(ctx.Msg.ChannelID, discordgo.PermissionSendMessages) {
  406. // Determine which guild(s)
  407. guilds := []string{ctx.Msg.GuildID} // default to origin
  408. if args := ctx.Args.After(1); args != "" { // specifics
  409. guilds = nil
  410. _guilds := strings.Split(args, ",")
  411. if len(_guilds) > 0 {
  412. for _, guild := range _guilds {
  413. guild = strings.TrimSpace(guild)
  414. guilds = append(guilds, guild)
  415. }
  416. }
  417. }
  418. for _, guild := range guilds {
  419. i := 0
  420. s := 0
  421. guildName := guild
  422. guildNameO := guild
  423. if guildInfo, err := bot.Guild(guild); err == nil {
  424. guildName = sanitize.Name(guildInfo.Name)
  425. guildNameO = guildInfo.Name
  426. }
  427. destination := "emojis" + string(os.PathSeparator) + guildName + string(os.PathSeparator)
  428. if err = os.MkdirAll(destination, 0755); err != nil {
  429. log.Println(lg("Command", "Emojis", color.HiRedString, "Error while creating destination folder \"%s\": %s", destination, err))
  430. } else {
  431. emojis, err := bot.GuildEmojis(guild)
  432. if err != nil {
  433. log.Println(lg("Command", "Emojis", color.HiRedString, "Failed to get server emojis:\t%s", err))
  434. } else {
  435. for _, emoji := range emojis {
  436. var message discordgo.Message
  437. message.ChannelID = ctx.Msg.ChannelID
  438. url := "https://cdn.discordapp.com/emojis/" + emoji.ID
  439. status, _ := handleDownload(
  440. downloadRequestStruct{
  441. InputURL: url,
  442. Filename: emoji.ID,
  443. Path: destination,
  444. Message: &message,
  445. FileTime: time.Now(),
  446. HistoryCmd: false,
  447. EmojiCmd: true,
  448. StartTime: time.Now(),
  449. })
  450. if status.Status == downloadSuccess {
  451. i++
  452. } else {
  453. s++
  454. log.Println(lg("Command", "Emojis", color.HiRedString,
  455. "Failed to download emoji \"%s\": \t[%d - %s] %v",
  456. url, status.Status, getDownloadStatusString(status.Status), status.Error))
  457. }
  458. }
  459. destinationOut := destination
  460. abs, err := filepath.Abs(destination)
  461. if err == nil {
  462. destinationOut = abs
  463. }
  464. _, err = replyEmbed(ctx.Msg, "Command — Emojis",
  465. fmt.Sprintf("`%d` emojis downloaded, `%d` skipped or failed\n• Destination: `%s`\n• Server: `%s`",
  466. i, s, destinationOut, guildNameO,
  467. ),
  468. )
  469. if err != nil {
  470. log.Println(lg("Command", "Emojis", color.HiRedString,
  471. "Failed to send status message for emoji downloads:\t%s", err))
  472. }
  473. }
  474. }
  475. }
  476. }
  477. } else {
  478. if !hasPerms(ctx.Msg.ChannelID, discordgo.PermissionSendMessages) {
  479. log.Println(lg("Command", "Emojis", color.HiRedString, fmtBotSendPerm, ctx.Msg.ChannelID))
  480. } else {
  481. if _, err := replyEmbed(ctx.Msg, "Command — Emojis", cmderrLackingBotAdminPerms); err != nil {
  482. log.Println(lg("Command", "Emojis", color.HiRedString, cmderrSendFailure, getUserIdentifier(*ctx.Msg.Author), err))
  483. }
  484. }
  485. log.Println(lg("Command", "Emojis", color.HiCyanString,
  486. "%s tried to download emojis but lacked bot admin perms.", getUserIdentifier(*ctx.Msg.Author)))
  487. }
  488. }
  489. }).Cat("Admin").Desc("Saves all server emojis to download destination")
  490. //#endregion
  491. // Handler for Command Router
  492. go bot.AddHandler(func(_ *discordgo.Session, m *discordgo.MessageCreate) {
  493. //NOTE: This setup makes it case-insensitive but message content will be lowercase, currently case sensitivity is not necessary.
  494. router.FindAndExecute(bot, strings.ToLower(config.CommandPrefix), bot.State.User.ID, messageToLower(m.Message))
  495. })
  496. return router
  497. }