pepebot/discord/simple.go

427 lines
14 KiB
Go
Raw Normal View History

2021-05-18 12:11:30 +02:00
package main
import (
"errors"
"fmt"
2021-09-08 22:35:03 +02:00
"io"
2021-12-17 15:38:34 +01:00
"math/rand"
2021-05-18 12:11:30 +02:00
"net/http"
2021-05-25 10:50:24 +02:00
"strconv"
2021-05-18 12:11:30 +02:00
"strings"
"time"
"github.com/bwmarrin/discordgo"
)
2021-05-25 13:17:30 +02:00
func (app *application) getPepeLink() (string, error) {
2021-09-08 23:01:19 +02:00
url := "http://" + app.pepeServer + "/pepe"
resp, err := http.Get(url)
2021-09-08 22:35:03 +02:00
if err != nil {
return "", err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
2021-05-25 13:17:30 +02:00
}
func (app *application) sendPepe(s *discordgo.Session, m *discordgo.MessageCreate) {
2022-01-10 14:08:53 +01:00
ch, err := s.Channel(m.ChannelID)
if err != nil {
app.errorLog.Print(err)
return
}
if !strings.Contains(ch.Topic, "pepebot allowed") {
app.errorLog.Print("Can't send pepe within this channel")
return
}
2021-05-25 13:17:30 +02:00
url, err := app.getPepeLink()
if err != nil {
app.errorLog.Print(err)
return
}
2021-05-18 12:11:30 +02:00
_, err = s.ChannelMessageSend(m.ChannelID, url)
if err != nil {
app.errorLog.Print(err)
2021-05-25 13:17:30 +02:00
return
2021-05-18 12:11:30 +02:00
}
}
func (app *application) sendCringe(s *discordgo.Session, m *discordgo.MessageCreate) {
_, err := s.ChannelMessageSend(m.ChannelID, "https://cdn.nicecock.eu/cringe.webm")
if err != nil {
app.errorLog.Print(err)
return
}
}
func (app *application) sendNigelGif(s *discordgo.Session, m *discordgo.MessageCreate) {
msg := "<@77516941199159296> kun je die gif verwijderen van die pickup truck die naar de camera rijdt want bij mij zorg ie voor dat discord opnieuw opstart. ik weet niet of iemand anders dit heeft maar als iemand weet hoe dit komt en een andere oplossing weet hoor ik het graag."
_, err := s.ChannelMessageSend(m.ChannelID, msg)
if err != nil {
app.errorLog.Print(err)
return
}
}
2021-05-31 14:31:13 +02:00
func (app *application) sendMonday(s *discordgo.Session, m *discordgo.MessageCreate) {
if time.Now().Weekday().String() != "Monday" {
_, err := s.ChannelMessageSend(m.ChannelID, "This command only works on mondays")
if err != nil {
app.errorLog.Print(err)
return
}
return
}
_, err := s.ChannelMessageSend(m.ChannelID, "https://www.youtube.com/watch?v=EkALyaMjoXw")
if err != nil {
app.errorLog.Print(err)
return
}
}
2021-05-18 12:37:26 +02:00
func (app *application) sendTuesday(s *discordgo.Session, m *discordgo.MessageCreate) {
if time.Now().Weekday().String() != "Tuesday" {
_, err := s.ChannelMessageSend(m.ChannelID, "This command only works on tuesdays")
if err != nil {
app.errorLog.Print(err)
return
}
2021-05-18 12:59:26 +02:00
return
2021-05-18 12:37:26 +02:00
}
_, err := s.ChannelMessageSend(m.ChannelID, "https://cdn.nicecock.eu/TBT.webm")
if err != nil {
app.errorLog.Print(err)
return
}
}
2021-05-18 12:54:04 +02:00
func (app *application) sendWednesday(s *discordgo.Session, m *discordgo.MessageCreate) {
2021-05-19 13:28:29 +02:00
_, month, day := time.Now().Date()
if month.String() == "May" && day == 19 {
_, err := s.ChannelMessageSend(m.ChannelID, "https://www.youtube.com/watch?v=z21HOwUk5oM")
2021-05-18 12:54:04 +02:00
if err != nil {
app.errorLog.Print(err)
return
}
2021-05-18 12:59:26 +02:00
return
2021-05-18 12:54:04 +02:00
}
2021-05-19 13:28:29 +02:00
if time.Now().Weekday().String() != "Wednesday" {
_, err := s.ChannelMessageSend(m.ChannelID, "This command only works on wednesdays")
2021-05-18 12:54:04 +02:00
if err != nil {
app.errorLog.Print(err)
return
}
return
}
_, err := s.ChannelMessageSend(m.ChannelID, "https://www.youtube.com/watch?v=du-TY1GUFGk")
if err != nil {
app.errorLog.Print(err)
return
}
}
2021-12-17 15:09:23 +01:00
func (app *application) sendFriday(s *discordgo.Session, m *discordgo.MessageCreate) {
if time.Now().Weekday().String() != "Friday" {
_, err := s.ChannelMessageSend(m.ChannelID, "This command only works on fridays")
if err != nil {
app.errorLog.Print(err)
return
}
return
}
_, err := s.ChannelMessageSend(m.ChannelID, "https://www.youtube.com/watch?v=kfVsfOSbJY0")
if err != nil {
app.errorLog.Print(err)
return
}
}
2021-05-18 12:56:16 +02:00
func (app *application) sendGithub(s *discordgo.Session, m *discordgo.MessageCreate) {
_, err := s.ChannelMessageSend(m.ChannelID, "My code is hosted publicly over at https://github.com/DutchEllie/pepebot")
if err != nil {
app.errorLog.Print(err)
return
}
}
2021-09-23 14:35:43 +02:00
func (app *application) sendPeski(s *discordgo.Session, m *discordgo.MessageCreate) {
_, err := s.ChannelMessageSend(m.ChannelID, "https://www.youtube.com/watch?v=P0jHTCJYm44")
if err != nil {
app.errorLog.Print(err)
return
}
}
2021-12-17 15:38:34 +01:00
func (app *application) sendProper(s *discordgo.Session, m *discordgo.MessageCreate) {
2022-01-10 14:08:53 +01:00
responseList := []string{"That's proper", "I use Gentoo", "So based", "That's best practice", "Emacs is the best", "LISP is so fucking based", "My website is here https://forestofunix.xyz\nIt's written in Lisp. It's so based!", "Python is LISP based"}
2021-12-17 15:38:34 +01:00
source := rand.NewSource(time.Now().UnixNano())
rng := rand.New(source)
index := rng.Intn(len(responseList))
2021-12-17 15:44:52 +01:00
_, err := s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("\"%s\"\n-Sebastiaan", responseList[index]))
2021-12-17 15:38:34 +01:00
if err != nil {
app.errorLog.Print(err)
return
}
}
2021-05-25 10:50:24 +02:00
func (app *application) sendManyPepes(s *discordgo.Session, m *discordgo.MessageCreate, splitCommand []string) {
2022-01-10 14:08:53 +01:00
ch, err := s.Channel(m.ChannelID)
if err != nil {
app.errorLog.Print(err)
return
}
if !strings.Contains(ch.Topic, "pepebot allowed") {
app.errorLog.Print("Can't send pepe within this channel")
return
}
2021-05-25 11:05:14 +02:00
override := false
2021-05-25 12:19:52 +02:00
/* [0] is !pepe, [1] is spam, [2] is amount, [3] is override*/
2021-05-25 11:05:14 +02:00
2021-05-25 10:50:24 +02:00
if len(splitCommand) <= 2 {
2021-05-25 12:19:52 +02:00
app.errorLog.Printf("spam command had no numeral argument")
2021-05-25 11:05:14 +02:00
s.ChannelMessageSend(m.ChannelID, "This command requires a numeral as a second argument, which is between 1 and 3")
2021-05-25 10:50:24 +02:00
return
}
2021-05-25 11:05:14 +02:00
if len(splitCommand) > 3 {
if splitCommand[3] == "override" {
/* Check if admin */
r, err := app.checkIfAdmin(s, m)
if err != nil {
app.errorLog.Print(err)
return
}
if r {
//s.ChannelMessageSend(m.ChannelID, "You have to be admin to override, not overriding")
override = true
}
2021-09-08 22:35:03 +02:00
2021-05-25 11:05:14 +02:00
}
}
2021-05-25 10:50:24 +02:00
val, err := strconv.Atoi(splitCommand[2])
2021-05-25 11:05:14 +02:00
if err != nil {
2021-05-25 12:19:52 +02:00
app.errorLog.Printf("spam command had a non-numeral as argument")
2021-05-25 11:05:14 +02:00
s.ChannelMessageSend(m.ChannelID, "This command requires a numeral as a second argument")
2021-05-25 10:50:24 +02:00
return
}
2021-05-25 11:05:14 +02:00
if (val <= 0 || val > 3) && !override {
s.ChannelMessageSend(m.ChannelID, "The amount has to be > 0 and < 4")
2021-05-25 11:15:41 +02:00
return
2021-05-25 11:05:14 +02:00
} else if val <= 0 && override {
s.ChannelMessageSend(m.ChannelID, "I know you're admin and all, but you still have to provide a positive integer amount of pepes to send...")
2021-05-25 11:15:41 +02:00
return
2021-05-25 11:05:14 +02:00
}
2021-05-25 11:15:41 +02:00
app.active = true
2021-05-25 13:17:30 +02:00
var msg string = ""
2021-05-25 10:50:24 +02:00
for i := 0; i < val; i++ {
2021-05-25 11:15:41 +02:00
if app.stop {
app.stop = false
break
}
2021-05-25 13:17:30 +02:00
link, err := app.getPepeLink()
if err != nil {
app.errorLog.Print(err)
return
}
2021-09-08 22:35:03 +02:00
2021-09-23 13:24:50 +02:00
if len(msg+link) > 512 {
2021-05-25 13:17:30 +02:00
s.ChannelMessageSend(m.ChannelID, msg)
msg = ""
time.Sleep(time.Millisecond * 500)
2021-09-08 22:35:03 +02:00
}
2021-05-25 13:17:30 +02:00
msg += link
msg += "\n"
2021-05-25 11:15:41 +02:00
}
2021-05-25 13:17:30 +02:00
s.ChannelMessageSend(m.ChannelID, msg)
2021-05-25 11:15:41 +02:00
app.active = false
}
func (app *application) stopRequest(s *discordgo.Session, m *discordgo.MessageCreate) {
if app.active {
app.stop = true
s.ChannelMessageSend(m.ChannelID, "Emergency stop called, hopefully I stop now")
} else {
app.stop = false
s.ChannelMessageSend(m.ChannelID, "But I wasn't doing anything!")
2021-05-25 10:50:24 +02:00
}
2021-09-08 22:35:03 +02:00
2021-05-25 10:50:24 +02:00
}
2021-05-18 12:11:30 +02:00
func (app *application) findTrigger(s *discordgo.Session, m *discordgo.MessageCreate) {
/* Finding for every word in the allBadWords map of string slices
Check if the message contains that word
if it doesn't continue,
if it does then get the word from the database, update the new time, format a message and send it */
2021-09-08 22:35:03 +02:00
for i := 0; i < len(app.allBadWords[m.GuildID]); i++ {
2021-05-18 12:11:30 +02:00
if strings.Contains(strings.ToLower(m.Content), strings.ToLower(app.allBadWords[m.GuildID][i])) {
/* Found the bad word */
word, err := app.badwords.GetWord(strings.ToLower(app.allBadWords[m.GuildID][i]), m.GuildID)
if err != nil {
app.errorLog.Print(err)
s.ChannelMessageSend(m.ChannelID, err.Error())
}
format := formatTimeCheck(word.LastSaid)
_, err = app.badwords.UpdateLastSaid(word.Word, word.ServerID)
if err != nil {
app.errorLog.Print(err)
return
}
user := m.Author.Mention()
eyesEmoji := ":eyes:"
message := fmt.Sprintf("%s mentioned the forbidden word '%s'. They broke a streak of %s...\nYou better watch out, I am always watching %s", user, word.Word, format, eyesEmoji)
2021-09-08 22:35:03 +02:00
_, err = s.ChannelMessageSend(m.ChannelID, message)
2021-05-18 12:11:30 +02:00
if err != nil {
app.errorLog.Print(err)
return
}
break
}
}
}
2021-09-08 22:35:03 +02:00
func formatTimeCheck(last time.Time) string {
2021-05-18 12:11:30 +02:00
now := time.Now()
sinceLast := now.Sub(last)
var realSeconds uint64 = uint64(sinceLast.Seconds())
var seconds, minutes, hours, days uint64
realBackup := realSeconds
2021-09-08 22:35:03 +02:00
days = realSeconds / (24 * 3600)
realSeconds -= days * (24 * 3600)
2021-05-18 12:11:30 +02:00
hours = realSeconds / 3600
realSeconds -= hours * 3600
minutes = realSeconds / 60
realSeconds -= minutes * 60
seconds = realSeconds
2021-09-08 22:35:03 +02:00
if realBackup < 60 {
if seconds == 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d second", seconds)
}
return fmt.Sprintf("%d seconds", seconds)
2021-09-08 22:35:03 +02:00
} else if realBackup > 60 && realBackup < 3600 {
if seconds == 1 && minutes == 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d minute and %d second", minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if minutes == 1 && seconds != 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d minute and %d seconds", minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if minutes != 1 && seconds == 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d minutes and %d second", minutes, seconds)
}
return fmt.Sprintf("%d minutes and %d seconds", minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if realBackup > 60 && realBackup < (24*3600) {
if hours == 1 && minutes == 1 && seconds == 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d hour, %d minute and %d second", hours, minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if hours == 1 && minutes == 1 && seconds != 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d hour, %d minute and %d seconds", hours, minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if hours == 1 && minutes != 1 && seconds == 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d hour, %d minutes and %d second", hours, minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if hours == 1 && minutes != 1 && seconds != 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d hour, %d minutes and %d seconds", hours, minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if hours != 1 && minutes == 1 && seconds == 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d hours, %d minute and %d second", hours, minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if hours != 1 && minutes == 1 && seconds != 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d hours, %d minute and %d seconds", hours, minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if hours != 1 && minutes != 1 && seconds == 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d hours, %d minutes and %d second", hours, minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if hours != 1 && minutes != 1 && seconds != 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d hours, %d minutes and %d seconds", hours, minutes, seconds)
}
return fmt.Sprintf("%d hours, %d minutes and %d seconds", hours, minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if realBackup > (24 * 3600) {
if days != 1 && hours != 1 && minutes != 1 && seconds != 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d days, %d hours, %d minutes and %d seconds", days, hours, minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if days != 1 && hours != 1 && minutes != 1 && seconds == 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d days, %d hours, %d minutes and %d second", days, hours, minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if days != 1 && hours != 1 && minutes == 1 && seconds != 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d days, %d hours, %d minute and %d seconds", days, hours, minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if days != 1 && hours != 1 && minutes == 1 && seconds == 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d days, %d hours, %d minute and %d second", days, hours, minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if days != 1 && hours == 1 && minutes != 1 && seconds != 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d days, %d hour, %d minutes and %d seconds", days, hours, minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if days != 1 && hours == 1 && minutes != 1 && seconds == 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d days, %d hour, %d minutes and %d second", days, hours, minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if days != 1 && hours == 1 && minutes == 1 && seconds != 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d days, %d hour, %d minute and %d seconds", days, hours, minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if days != 1 && hours == 1 && minutes == 1 && seconds == 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d days, %d hour, %d minute and %d second", days, hours, minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if days == 1 && hours != 1 && minutes != 1 && seconds != 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d day, %d hours, %d minutes and %d seconds", days, hours, minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if days == 1 && hours != 1 && minutes != 1 && seconds == 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d day, %d hours, %d minutes and %d second", days, hours, minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if days == 1 && hours != 1 && minutes == 1 && seconds != 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d day, %d hours, %d minute and %d seconds", days, hours, minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if days == 1 && hours != 1 && minutes == 1 && seconds == 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d day, %d hours, %d minute and %d second", days, hours, minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if days == 1 && hours == 1 && minutes != 1 && seconds != 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d day, %d hour, %d minutes and %d seconds", days, hours, minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if days == 1 && hours == 1 && minutes != 1 && seconds == 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d day, %d hour, %d minutes and %d second", days, hours, minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if days == 1 && hours == 1 && minutes == 1 && seconds != 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d day, %d hour, %d minute and %d seconds", days, hours, minutes, seconds)
2021-09-08 22:35:03 +02:00
} else if days == 1 && hours == 1 && minutes == 1 && seconds == 1 {
2021-05-18 12:11:30 +02:00
return fmt.Sprintf("%d day, %d hour, %d minute and %d second", days, hours, minutes, seconds)
}
return fmt.Sprintf("%d days, %d hours, %d minutes and %d seconds", days, hours, minutes, seconds)
}
return "error"
}
func (app *application) checkIfAdmin(s *discordgo.Session, m *discordgo.MessageCreate) (bool, error) {
authorMemberInfo, err := s.GuildMember(m.GuildID, m.Author.ID)
if err != nil {
return false, err
}
roleIDs, err := app.adminroles.GetAdminRoleIDs()
if err != nil {
return false, err
}
for i := 0; i < len(authorMemberInfo.Roles); i++ {
for j := 0; j < len(roleIDs); j++ {
if authorMemberInfo.Roles[i] == roleIDs[j] {
return true, nil
}
}
}
app.infoLog.Printf("The user %s tried to perform an admin command without an admin role, purge them", m.Author)
_, err = s.ChannelMessageSend(m.ChannelID, "You aren't authorized to perform this function, this incident has been reported.")
if err != nil {
return false, err
}
return false, nil
}
2021-09-08 22:35:03 +02:00
func (app *application) contextLength(splitCommand []string) error {
2021-05-18 12:11:30 +02:00
if !(len(splitCommand) > 2) {
app.errorLog.Printf("The command's context was not enough.\n")
return errors.New("not enough context")
}
return nil
2021-05-18 12:49:30 +02:00
}
func (app *application) successMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
_, err := s.ChannelMessageSend(m.ChannelID, "Success!")
if err != nil {
app.unknownError(err, s, true, m.ChannelID)
return
}
2021-09-08 22:35:03 +02:00
}