gofmt all the things

This commit is contained in:
kageru 2019-01-10 00:16:39 +01:00
parent 6e7229d787
commit 4566ffc230
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
7 changed files with 381 additions and 387 deletions

View File

@ -4,6 +4,7 @@ Updates are listed in reverse chronological order.
### 1.4 (dev) ### 1.4 (dev)
- a copy of each deleted message is now send via DM to the author (suggested by CommanderLook) - a copy of each deleted message is now send via DM to the author (suggested by CommanderLook)
- seasonal fluff - seasonal fluff
- finally use gofmt
### 1.3 ### 1.3
- use global array of pointers to commands to allow easier modification and avoid unnecessary memcpy - use global array of pointers to commands to allow easier modification and avoid unnecessary memcpy

View File

@ -1,257 +1,255 @@
package main package main
import ( import (
"fmt" "fmt"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"github.com/deckarep/golang-set" "github.com/deckarep/golang-set"
"strings" "log"
"time" "regexp"
"log" "strings"
"regexp" "time"
) )
type CommandType int type CommandType int
// These are used to specify Command.CommandType when registering new commands // These are used to specify Command.CommandType when registering new commands
const ( const (
CommandTypePrefix CommandType = 0 CommandTypePrefix CommandType = 0
CommandTypeFullMatch CommandType = 1 CommandTypeFullMatch CommandType = 1
CommandTypeRegex CommandType = 2 CommandTypeRegex CommandType = 2
CommandTypeContains CommandType = 3 CommandTypeContains CommandType = 3
) )
/* /*
This struct represents a command object. This struct represents a command object.
The options should be self-explanatory, but they are also explained in the readme. The options should be self-explanatory, but they are also explained in the readme.
A struct can be initialized by passing any number of its attributes as parameters. A struct can be initialized by passing any number of its attributes as parameters.
Everything not set will be set to the go-usual defaults ("" for string, 0 for int, false for bool, nil for the rest) Everything not set will be set to the go-usual defaults ("" for string, 0 for int, false for bool, nil for the rest)
Any command that has a Trigger is valid (but useless if nothing else is specified) Any command that has a Trigger is valid (but useless if nothing else is specified)
*/ */
type Command struct { type Command struct {
Trigger string // must be specified Trigger string // must be specified
Output string // no output if unspecified Output string // no output if unspecified
OutputEmbed *discordgo.MessageEmbed // no embed output if unspecified OutputEmbed *discordgo.MessageEmbed // no embed output if unspecified
Type CommandType // defaults to Prefix Type CommandType // defaults to Prefix
Cooldown int // defaults to 0 (no cooldown) Cooldown int // defaults to 0 (no cooldown)
OutputIsReply bool OutputIsReply bool
RequiresMention bool RequiresMention bool
DeleteInput bool DeleteInput bool
DMOnly bool DMOnly bool
AdminOnly bool AdminOnly bool
IgnoreCase bool IgnoreCase bool
// for custom commands that go beyond prints and deletions // for custom commands that go beyond prints and deletions
Function func(*discordgo.Session, *discordgo.MessageCreate) Function func(*discordgo.Session, *discordgo.MessageCreate)
UsersOnCooldown mapset.Set // don’t set this manually (it’s overwritten anyway) UsersOnCooldown mapset.Set // don’t set this manually (it’s overwritten anyway)
} }
// Performs basic input validation on a given command and adds it to the global command array // Performs basic input validation on a given command and adds it to the global command array
func registerCommand(command Command) { func registerCommand(command Command) {
if command.Trigger == "" { if command.Trigger == "" {
fmt.Println("Cannot register a command with no trigger. Skipping.") fmt.Println("Cannot register a command with no trigger. Skipping.")
return return
} }
if command.IgnoreCase { if command.IgnoreCase {
command.Trigger = strings.ToLower(command.Trigger) command.Trigger = strings.ToLower(command.Trigger)
} }
command.UsersOnCooldown = mapset.NewSet() command.UsersOnCooldown = mapset.NewSet()
commands = append(commands, &command) commands = append(commands, &command)
} }
/* /*
Any message that the bot can read is evaluated here. Any message that the bot can read is evaluated here.
The message is matched against each of the command triggers depending on the respective match type. The message is matched against each of the command triggers depending on the respective match type.
If one of the commands matches, execute that command and return. If one of the commands matches, execute that command and return.
Only one command can be executed per message. Earlier defined commands take precedence. Only one command can be executed per message. Earlier defined commands take precedence.
This is a deliberate choice (for now). This is a deliberate choice (for now).
*/ */
func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) { func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID { if m.Author.ID == s.State.User.ID {
log.Printf("<Self> %s", m.Content) log.Printf("<Self> %s", m.Content)
return return
} }
for _, command := range commands { for _, command := range commands {
content := m.Content content := m.Content
if command.IgnoreCase { if command.IgnoreCase {
content = strings.ToLower(content) content = strings.ToLower(content)
} }
if command.RequiresMention { if command.RequiresMention {
command.Trigger = fmt.Sprintf(command.Trigger, s.State.User.ID) command.Trigger = fmt.Sprintf(command.Trigger, s.State.User.ID)
} }
switch command.Type { switch command.Type {
case CommandTypePrefix: case CommandTypePrefix:
if strings.HasPrefix(content, command.Trigger) { if strings.HasPrefix(content, command.Trigger) {
executeCommand(s, m, command) executeCommand(s, m, command)
return return
} }
case CommandTypeFullMatch: case CommandTypeFullMatch:
if content == command.Trigger { if content == command.Trigger {
executeCommand(s, m, command) executeCommand(s, m, command)
return return
} }
case CommandTypeRegex: case CommandTypeRegex:
match, _ := regexp.MatchString(command.Trigger, content) match, _ := regexp.MatchString(command.Trigger, content)
if match { if match {
executeCommand(s, m, command) executeCommand(s, m, command)
return return
} }
case CommandTypeContains: case CommandTypeContains:
if strings.Contains(content, command.Trigger) { if strings.Contains(content, command.Trigger) {
executeCommand(s, m, command) executeCommand(s, m, command)
return return
} }
} }
} }
} }
/* /*
Executes the given command on the given message and session. Executes the given command on the given message and session.
Sets command cooldowns if necessary and also clears them again. Sets command cooldowns if necessary and also clears them again.
*/ */
func executeCommand(session *discordgo.Session, message *discordgo.MessageCreate, command *Command) { func executeCommand(session *discordgo.Session, message *discordgo.MessageCreate, command *Command) {
if isAdmin(message.Author) || // no restrictions for admins if isAdmin(message.Author) || // no restrictions for admins
(!command.AdminOnly && (isDM(session, message) || !command.UsersOnCooldown.Contains(message.Author.ID)) && (!command.AdminOnly && (isDM(session, message) || !command.UsersOnCooldown.Contains(message.Author.ID)) &&
(!command.DMOnly || isDM(session, message))) { (!command.DMOnly || isDM(session, message))) {
log.Printf("Executed command %s triggered by user %s", command.Trigger, userToString(message.Author)) log.Printf("Executed command %s triggered by user %s", command.Trigger, userToString(message.Author))
if command.Cooldown > 0 && !isDM(session, message) && !isAdmin(message.Author) { if command.Cooldown > 0 && !isDM(session, message) && !isAdmin(message.Author) {
command.UsersOnCooldown.Add(message.Author.ID) command.UsersOnCooldown.Add(message.Author.ID)
go removeCooldown(command, message.Author.ID) go removeCooldown(command, message.Author.ID)
} }
if command.Function == nil { if command.Function == nil {
// simple reply // simple reply
if command.OutputEmbed == nil { if command.OutputEmbed == nil {
messageContent := generateReply(message, command) messageContent := generateReply(message, command)
session.ChannelMessageSend(message.ChannelID, messageContent) session.ChannelMessageSend(message.ChannelID, messageContent)
} else { } else {
session.ChannelMessageSendEmbed(message.ChannelID, command.OutputEmbed) session.ChannelMessageSendEmbed(message.ChannelID, command.OutputEmbed)
} }
if command.DeleteInput { if command.DeleteInput {
deleteAndSendViaDM(session, message) deleteAndSendViaDM(session, message)
} }
} else { } else {
// execute custom function // execute custom function
command.Function(session, message) command.Function(session, message)
} }
} else { } else {
log.Printf("Denied command %s to user %s.", command.Trigger, userToString(message.Author)) log.Printf("Denied command %s to user %s.", command.Trigger, userToString(message.Author))
} }
} }
func removeCooldown(command *Command, uid string) { func removeCooldown(command *Command, uid string) {
time.Sleep(time.Duration(command.Cooldown) * time.Second) time.Sleep(time.Duration(command.Cooldown) * time.Second)
if command.UsersOnCooldown.Contains(uid) { if command.UsersOnCooldown.Contains(uid) {
command.UsersOnCooldown.Remove(uid) command.UsersOnCooldown.Remove(uid)
} }
} }
func deleteAndSendViaDM(s *discordgo.Session, message *discordgo.MessageCreate) { func deleteAndSendViaDM(s *discordgo.Session, message *discordgo.MessageCreate) {
s.ChannelMessageDelete(message.ChannelID, message.ID) s.ChannelMessageDelete(message.ChannelID, message.ID)
dm := getDMChannelFromMessage(s, message) dm := getDMChannelFromMessage(s, message)
s.ChannelMessageSend(dm.ID, "Deine Nachricht wurde gelöscht, weil sie ein verbotenes Wort enthielt. Falls du sie editieren und erneut abschicken willst, hier die Nachricht:") s.ChannelMessageSend(dm.ID, "Deine Nachricht wurde gelöscht, weil sie ein verbotenes Wort enthielt. Falls du sie editieren und erneut abschicken willst, hier die Nachricht:")
s.ChannelMessageSend(dm.ID, message.Content) s.ChannelMessageSend(dm.ID, message.Content)
} }
func generateReply(message *discordgo.MessageCreate, command *Command) string { func generateReply(message *discordgo.MessageCreate, command *Command) string {
output := command.Output output := command.Output
if command.OutputIsReply { if command.OutputIsReply {
output = fmt.Sprintf(output, message.Author.ID) output = fmt.Sprintf(output, message.Author.ID)
} }
return output return output
} }
/* /*
Any message passed to this method will be redirected to config.ModChannel. Any message passed to this method will be redirected to config.ModChannel.
This is useful for anonymous complaints or similar messages. This is useful for anonymous complaints or similar messages.
*/ */
func redirectComplaint(s *discordgo.Session, m *discordgo.MessageCreate) { func redirectComplaint(s *discordgo.Session, m *discordgo.MessageCreate) {
embed := &discordgo.MessageEmbed { embed := &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{}, Author: &discordgo.MessageEmbedAuthor{},
Color: 0xbb0000, Color: 0xbb0000,
Description: m.Content, Description: m.Content,
} }
s.ChannelMessageSendEmbed(config.ModChannel, embed) s.ChannelMessageSendEmbed(config.ModChannel, embed)
dm, _ := s.UserChannelCreate(m.Author.ID) dm, _ := s.UserChannelCreate(m.Author.ID)
s.ChannelMessageSend(dm.ID, config.ComplaintReceivedMessage) s.ChannelMessageSend(dm.ID, config.ComplaintReceivedMessage)
} }
// copy paste programming btw :haHAA: // copy paste programming btw :haHAA:
func redirectComplaintToDM(s *discordgo.Session, m *discordgo.MessageCreate) { func redirectComplaintToDM(s *discordgo.Session, m *discordgo.MessageCreate) {
embed := &discordgo.MessageEmbed { embed := &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{}, Author: &discordgo.MessageEmbedAuthor{},
Color: 0xbb0000, Color: 0xbb0000,
Description: m.Content, Description: m.Content,
} }
dm_target, _ := s.UserChannelCreate("190958368301645824") dm_target, _ := s.UserChannelCreate("190958368301645824")
s.ChannelMessageSendEmbed(dm_target.ID, embed) s.ChannelMessageSendEmbed(dm_target.ID, embed)
dm, _ := s.UserChannelCreate(m.Author.ID) dm, _ := s.UserChannelCreate(m.Author.ID)
s.ChannelMessageSend(dm.ID, config.ComplaintReceivedMessage) s.ChannelMessageSend(dm.ID, config.ComplaintReceivedMessage)
} }
func echoMessage(s *discordgo.Session, m *discordgo.MessageCreate) { func echoMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
s.ChannelMessageSend(m.ChannelID, m.Content) s.ChannelMessageSend(m.ChannelID, m.Content)
} }
func giveAgeRole(s *discordgo.Session, m *discordgo.MessageCreate) { func giveAgeRole(s *discordgo.Session, m *discordgo.MessageCreate) {
Member, _ := s.GuildMember(config.ServerID, m.Author.ID) Member, _ := s.GuildMember(config.ServerID, m.Author.ID)
dm, _ := s.UserChannelCreate(Member.User.ID) dm, _ := s.UserChannelCreate(Member.User.ID)
for command, role := range config.RoleCommands { for command, role := range config.RoleCommands {
if m.Content == command { if m.Content == command {
// Found the command that was triggered // Found the command that was triggered
// This is a restriction imposed by my own wrapper, // This is a restriction imposed by my own wrapper,
// but working around it is not actually necessary for performance and makes the code uglier in other places. // but working around it is not actually necessary for performance and makes the code uglier in other places.
for _, newRole := range config.RoleCommands { for _, newRole := range config.RoleCommands {
for _, curRole := range Member.Roles { for _, curRole := range Member.Roles {
// If the user already has one of the available roles, tell him and exit // If the user already has one of the available roles, tell him and exit
if newRole == curRole { if newRole == curRole {
if curRole == role { if curRole == role {
// User is trying to get the role they already have // User is trying to get the role they already have
s.ChannelMessageSend(dm.ID, "Baka, die Rolle hast du doch schon.") s.ChannelMessageSend(dm.ID, "Baka, die Rolle hast du doch schon.")
log.Printf("Denied Role %s to %s. User already has %s", roleName(s.State, curRole), userToString(m.Author), roleName(s.State, curRole)) log.Printf("Denied Role %s to %s. User already has %s", roleName(s.State, curRole), userToString(m.Author), roleName(s.State, curRole))
} else { } else {
s.ChannelMessageSend(dm.ID, "Baka, du kannst nur eine der Rollen haben.") s.ChannelMessageSend(dm.ID, "Baka, du kannst nur eine der Rollen haben.")
log.Printf("Denied Role %s to %s. User already has %s", roleName(s.State, curRole), userToString(m.Author), roleName(s.State, curRole)) log.Printf("Denied Role %s to %s. User already has %s", roleName(s.State, curRole), userToString(m.Author), roleName(s.State, curRole))
} }
return return
} }
} }
} }
log.Printf("Giving Role %s to %s", roleName(s.State, role), userToString(m.Author)) log.Printf("Giving Role %s to %s", roleName(s.State, role), userToString(m.Author))
s.ChannelMessageSend(dm.ID, "Haaai, Ryoukai desu~") s.ChannelMessageSend(dm.ID, "Haaai, Ryoukai desu~")
s.GuildMemberRoleAdd(config.ServerID, m.Author.ID, role) s.GuildMemberRoleAdd(config.ServerID, m.Author.ID, role)
} }
} }
} }
func getHelpEmbed() *discordgo.MessageEmbed { func getHelpEmbed() *discordgo.MessageEmbed {
commandList := "Im Folgenden findest du eine automatisch generierte Liste aller Commands. Um herauszufinden, was sie tun, probiere sie aus oder lies den Source Code (siehe unten).\n```- !complain\n- !scomplain\n" commandList := "Im Folgenden findest du eine automatisch generierte Liste aller Commands. Um herauszufinden, was sie tun, probiere sie aus oder lies den Source Code (siehe unten).\n```- !complain\n- !scomplain\n"
for _, command := range commands { for _, command := range commands {
if command.Type != CommandTypeRegex && !command.AdminOnly && !command.DMOnly { if command.Type != CommandTypeRegex && !command.AdminOnly && !command.DMOnly {
commandList += "- " + command.Trigger + "\n" commandList += "- " + command.Trigger + "\n"
} }
} }
commandList += "```" commandList += "```"
embed := &discordgo.MessageEmbed{ embed := &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{}, Author: &discordgo.MessageEmbedAuthor{},
Color: 0xffb90f, Color: 0xffb90f,
Description: "__Hilfe__", Description: "__Hilfe__",
Fields: []*discordgo.MessageEmbedField { Fields: []*discordgo.MessageEmbedField{
&discordgo.MessageEmbedField { &discordgo.MessageEmbedField{
Name: "__Commands__", Name: "__Commands__",
Value: commandList, Value: commandList,
Inline: true, Inline: true,
}, },
&discordgo.MessageEmbedField { &discordgo.MessageEmbedField{
Name: "__Bugs__", Name: "__Bugs__",
Value: fmt.Sprintf("Bei Fragen zum Bot, Vorschlägen, Bugs etc. wende dich bitte an <@%s> oder öffne eine Issue auf https://git.kageru.moe/kageru/discord-selphybot.", config.Admins[0]), Value: fmt.Sprintf("Bei Fragen zum Bot, Vorschlägen, Bugs etc. wende dich bitte an <@%s> oder öffne eine Issue auf https://git.kageru.moe/kageru/discord-selphybot.", config.Admins[0]),
Inline: true, Inline: true,
}, },
}, },
Thumbnail: &discordgo.MessageEmbedThumbnail{ Thumbnail: &discordgo.MessageEmbedThumbnail{
URL: "https://static-cdn.jtvnw.net/emoticons/v1/1068185/3.0", URL: "https://static-cdn.jtvnw.net/emoticons/v1/1068185/3.0",
}, },
} }
return embed return embed
} }

View File

@ -1,40 +1,38 @@
package main package main
import ( import (
"os" "encoding/json"
"encoding/json" "os"
) )
type Embed struct { type Embed struct {
Message string Message string
QuestionsTitle string QuestionsTitle string
QuestionsText string QuestionsText string
BugsTitle string BugsTitle string
BugsText string BugsText string
Image string Image string
} }
type Config struct { type Config struct {
Admins []string Admins []string
ServerID string ServerID string
LockedRoleID string LockedRoleID string
Token string Token string
WelcomeChannel string WelcomeChannel string
GeneralChannel string GeneralChannel string
SendWelcomeDM bool SendWelcomeDM bool
RequireAccept bool RequireAccept bool
ComplaintReceivedMessage string ComplaintReceivedMessage string
ModChannel string ModChannel string
WelcomeEmbed Embed WelcomeEmbed Embed
RoleCommands map[string]string RoleCommands map[string]string
} }
func readConfig() Config { func readConfig() Config {
file, _ := os.Open("config.json") file, _ := os.Open("config.json")
conf := Config{} conf := Config{}
json.NewDecoder(file).Decode(&conf) json.NewDecoder(file).Decode(&conf)
file.Close() file.Close()
return conf return conf
} }

View File

@ -1,55 +1,54 @@
package main package main
import ( import (
"fmt" "fmt"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"log" "log"
) )
func onJoin(s *discordgo.Session, member *discordgo.GuildMemberAdd) { func onJoin(s *discordgo.Session, member *discordgo.GuildMemberAdd) {
if !member.User.Bot && config.RequireAccept { if !member.User.Bot && config.RequireAccept {
s.GuildMemberRoleAdd(config.ServerID, member.User.ID, config.LockedRoleID) s.GuildMemberRoleAdd(config.ServerID, member.User.ID, config.LockedRoleID)
} }
if !member.User.Bot && config.SendWelcomeDM { if !member.User.Bot && config.SendWelcomeDM {
dm, err := s.UserChannelCreate(member.User.ID) dm, err := s.UserChannelCreate(member.User.ID)
if err != nil { if err != nil {
log.Println(fmt.Sprintf("Error creating DM with %s", userToString(member.User), err)) log.Println(fmt.Sprintf("Error creating DM with %s", userToString(member.User), err))
} else { } else {
embed := getWelcomeEmbed() embed := getWelcomeEmbed()
_, err = s.ChannelMessageSendEmbed(dm.ID, embed) _, err = s.ChannelMessageSendEmbed(dm.ID, embed)
if err != nil { if err != nil {
log.Println(fmt.Sprintf("Error sending DM to %s", userToString(member.User), err)) log.Println(fmt.Sprintf("Error sending DM to %s", userToString(member.User), err))
} }
} }
if err != nil { if err != nil {
// if any of the preceding operations produced an error // if any of the preceding operations produced an error
log.Printf("Sending welcome @mention at %s", userToString(member.User)) log.Printf("Sending welcome @mention at %s", userToString(member.User))
s.ChannelMessageSend(config.GeneralChannel, fmt.Sprintf("Wilkommen <@%s>. Bitte aktiviere vorübergehend DMs für diesen Server und sende eine Nachricht mit !welcome an mich.", member.User.ID)) s.ChannelMessageSend(config.GeneralChannel, fmt.Sprintf("Wilkommen <@%s>. Bitte aktiviere vorübergehend DMs für diesen Server und sende eine Nachricht mit !welcome an mich.", member.User.ID))
} }
} }
log.Printf("User joined: %s", userToString(member.User)) log.Printf("User joined: %s", userToString(member.User))
} }
func onDM(s *discordgo.Session, m *discordgo.MessageCreate) { func onDM(s *discordgo.Session, m *discordgo.MessageCreate) {
log.Printf("Received DM from %s with content: “%s”", userToString(m.Author), m.Content) log.Printf("Received DM from %s with content: “%s”", userToString(m.Author), m.Content)
fmt.Sprintf("Received DM from %s with content: “%s”", userToString(m.Author), m.Content) fmt.Sprintf("Received DM from %s with content: “%s”", userToString(m.Author), m.Content)
Member, _ := s.GuildMember(config.ServerID, m.Author.ID) Member, _ := s.GuildMember(config.ServerID, m.Author.ID)
dm, _ := s.UserChannelCreate(Member.User.ID) dm, _ := s.UserChannelCreate(Member.User.ID)
for comm, role := range config.RoleCommands { for comm, role := range config.RoleCommands {
if m.Content == comm { if m.Content == comm {
for _, irole := range config.RoleCommands { for _, irole := range config.RoleCommands {
for _, mrole := range Member.Roles { for _, mrole := range Member.Roles {
if irole == mrole { if irole == mrole {
s.ChannelMessageSend(dm.ID, "Baka, du kannst nur eine der Rollen haben.") s.ChannelMessageSend(dm.ID, "Baka, du kannst nur eine der Rollen haben.")
log.Printf("Denied Role %s to %s. User already has %s", roleName(s.State, irole), userToString(m.Author), roleName(s.State, irole)) log.Printf("Denied Role %s to %s. User already has %s", roleName(s.State, irole), userToString(m.Author), roleName(s.State, irole))
return return
} }
} }
} }
log.Printf("Giving Role %s to %s", roleName(s.State, role), userToString(m.Author)) log.Printf("Giving Role %s to %s", roleName(s.State, role), userToString(m.Author))
s.ChannelMessageSend(dm.ID, "Haaai, Ryoukai desu~") s.ChannelMessageSend(dm.ID, "Haaai, Ryoukai desu~")
s.GuildMemberRoleAdd(config.ServerID, m.Author.ID, role) s.GuildMemberRoleAdd(config.ServerID, m.Author.ID, role)
} }
} }
} }

View File

@ -1,45 +1,45 @@
package main package main
import ( import (
"github.com/bwmarrin/discordgo" "fmt"
"log" "github.com/bwmarrin/discordgo"
"fmt" "log"
) )
func unlockUser(s *discordgo.Session, id string) { func unlockUser(s *discordgo.Session, id string) {
s.GuildMemberRoleRemove(config.ServerID, id, config.LockedRoleID) s.GuildMemberRoleRemove(config.ServerID, id, config.LockedRoleID)
log.Printf("Removed lock from user: %s", userToString(getUser(s, id))) log.Printf("Removed lock from user: %s", userToString(getUser(s, id)))
} }
func userToString(u *discordgo.User) string { func userToString(u *discordgo.User) string {
return fmt.Sprintf("%s#%s (ID: %s)", u.Username, u.Discriminator, u.ID) return fmt.Sprintf("%s#%s (ID: %s)", u.Username, u.Discriminator, u.ID)
} }
func roleName(s *discordgo.State, rid string) string { func roleName(s *discordgo.State, rid string) string {
role, _ := s.Role(config.ServerID, rid) role, _ := s.Role(config.ServerID, rid)
return role.Name return role.Name
} }
func channelToString(c *discordgo.Channel) string { func channelToString(c *discordgo.Channel) string {
return fmt.Sprintf("%s (ID: %s) on %s", c.Name, c.ID, c.GuildID) return fmt.Sprintf("%s (ID: %s) on %s", c.Name, c.ID, c.GuildID)
} }
func messageToString(m *discordgo.Message) string { func messageToString(m *discordgo.Message) string {
return fmt.Sprintf("<%s#%s>: %s", m.Author.Username, m.Author.Discriminator, m.Content) return fmt.Sprintf("<%s#%s>: %s", m.Author.Username, m.Author.Discriminator, m.Content)
} }
func getChannel(s *discordgo.State, cid string) *discordgo.Channel { func getChannel(s *discordgo.State, cid string) *discordgo.Channel {
channel, _ := s.Channel(cid) channel, _ := s.Channel(cid)
return channel return channel
} }
func getUser(s *discordgo.Session, uid string) *discordgo.User { func getUser(s *discordgo.Session, uid string) *discordgo.User {
user, _ := s.User(uid) user, _ := s.User(uid)
return user return user
} }
func isDM(s *discordgo.Session, m *discordgo.MessageCreate) bool { func isDM(s *discordgo.Session, m *discordgo.MessageCreate) bool {
return (getChannel(s.State, m.ChannelID).Type == discordgo.ChannelTypeDM) return (getChannel(s.State, m.ChannelID).Type == discordgo.ChannelTypeDM)
} }
func getDMChannelFromMessage(s *discordgo.Session, m *discordgo.MessageCreate) *discordgo.Channel { func getDMChannelFromMessage(s *discordgo.Session, m *discordgo.MessageCreate) *discordgo.Channel {
@ -48,10 +48,10 @@ func getDMChannelFromMessage(s *discordgo.Session, m *discordgo.MessageCreate) *
} }
func isAdmin(u *discordgo.User) bool { func isAdmin(u *discordgo.User) bool {
for _, admin := range config.Admins { for _, admin := range config.Admins {
if u.ID == admin { if u.ID == admin {
return true return true
} }
} }
return false return false
} }

128
main.go
View File

@ -1,89 +1,87 @@
package main package main
import ( import (
"fmt" "fmt"
"os/signal" "github.com/bwmarrin/discordgo"
"os" "log"
"syscall" "os"
"log" "os/signal"
"github.com/bwmarrin/discordgo" "syscall"
) )
var config = readConfig() var config = readConfig()
var commands []*Command var commands []*Command
func main() { func main() {
dg, err := discordgo.New("Bot " + config.Token) dg, err := discordgo.New("Bot " + config.Token)
if err != nil { if err != nil {
fmt.Println("error: ", err) fmt.Println("error: ", err)
return return
} }
defer dg.Close() defer dg.Close()
dg.AddHandler(evaluateMessage) dg.AddHandler(evaluateMessage)
dg.AddHandler(onJoin) dg.AddHandler(onJoin)
err = dg.Open() err = dg.Open()
if err != nil { if err != nil {
fmt.Println("No connection:\n", err) fmt.Println("No connection:\n", err)
return return
} }
f, err := os.OpenFile("selphybot.log", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666) f, err := os.OpenFile("selphybot.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil { if err != nil {
fmt.Println("Error opening log file:\n", err) fmt.Println("Error opening log file:\n", err)
} }
defer f.Close() defer f.Close()
log.SetOutput(f) log.SetOutput(f)
dg.UpdateStatus(0, "!help") dg.UpdateStatus(0, "!help")
addCommands() addCommands()
fmt.Println("Bot running. selphyWoo") fmt.Println("Bot running. selphyWoo")
log.Println("Bot running. selphyWoo") log.Println("Bot running. selphyWoo")
sc := make(chan os.Signal, 1) sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc <-sc
fmt.Println("Exiting...") fmt.Println("Exiting...")
log.Println("Exiting...") log.Println("Exiting...")
} }
// I’ll just put all of the commands here for now. // I’ll just put all of the commands here for now.
func addCommands() { func addCommands() {
// Moderation // Moderation
registerCommand(Command{Trigger: "^[^`]*([()|DoOvVcC][-=^']?;|;[-=^']?[()|DoOpPvVcC3]|:wink:|😉)[^`]*$", Output: "<@%s> Oboe!", DeleteInput: true, OutputIsReply: true, Type: CommandTypeRegex}) registerCommand(Command{Trigger: "^[^`]*([()|DoOvVcC][-=^']?;|;[-=^']?[()|DoOpPvVcC3]|:wink:|😉)[^`]*$", Output: "<@%s> Oboe!", DeleteInput: true, OutputIsReply: true, Type: CommandTypeRegex})
registerCommand(Command{Trigger: "(\\s|\n|^)[nN][hH]([ ?.,\n]|$)", Output: "<@%s> „nh“ ist kein Wort, du Oboe!", DeleteInput: true, OutputIsReply: true, Type: CommandTypeRegex}) registerCommand(Command{Trigger: "(\\s|\n|^)[nN][hH]([ ?.,\n]|$)", Output: "<@%s> „nh“ ist kein Wort, du Oboe!", DeleteInput: true, OutputIsReply: true, Type: CommandTypeRegex})
registerCommand(Command{Trigger: "einzigste", Output: "<@%s> Es heißt „einzige“, du Tuba.", DeleteInput: true, OutputIsReply: true, Type: CommandTypeContains}) registerCommand(Command{Trigger: "einzigste", Output: "<@%s> Es heißt „einzige“, du Tuba.", DeleteInput: true, OutputIsReply: true, Type: CommandTypeContains})
registerCommand(Command{Trigger: "!complain", Type: CommandTypePrefix, DMOnly: true, Function: redirectComplaint}) registerCommand(Command{Trigger: "!complain", Type: CommandTypePrefix, DMOnly: true, Function: redirectComplaint})
registerCommand(Command{Trigger: "!scomplain", Type: CommandTypePrefix, DMOnly: true, Function: redirectComplaintToDM}) registerCommand(Command{Trigger: "!scomplain", Type: CommandTypePrefix, DMOnly: true, Function: redirectComplaintToDM})
registerCommand(Command{Trigger: "!beschwerde", Type: CommandTypePrefix, DMOnly: true, Function: redirectComplaint}) registerCommand(Command{Trigger: "!beschwerde", Type: CommandTypePrefix, DMOnly: true, Function: redirectComplaint})
for comm, _ := range config.RoleCommands { for comm, _ := range config.RoleCommands {
registerCommand(Command{Trigger: comm, Type: CommandTypeFullMatch, DMOnly: true, Function: giveAgeRole}) registerCommand(Command{Trigger: comm, Type: CommandTypeFullMatch, DMOnly: true, Function: giveAgeRole})
} }
// Misc commands // Misc commands
registerCommand(Command{Trigger: "o/", Output: "\\o", Type: CommandTypeFullMatch, Cooldown: 10}) registerCommand(Command{Trigger: "o/", Output: "\\o", Type: CommandTypeFullMatch, Cooldown: 10})
registerCommand(Command{Trigger: "\\o", Output: "o/", Type: CommandTypeFullMatch, Cooldown: 10}) registerCommand(Command{Trigger: "\\o", Output: "o/", Type: CommandTypeFullMatch, Cooldown: 10})
registerCommand(Command{Trigger: "\\o/", Output: "/o\\", Type: CommandTypeFullMatch, Cooldown: 10}) registerCommand(Command{Trigger: "\\o/", Output: "/o\\", Type: CommandTypeFullMatch, Cooldown: 10})
registerCommand(Command{Trigger: "/o\\", Output: "\\o/", Type: CommandTypeFullMatch, Cooldown: 10}) registerCommand(Command{Trigger: "/o\\", Output: "\\o/", Type: CommandTypeFullMatch, Cooldown: 10})
registerCommand(Command{Trigger: "!heil", Output: "(ノ・ェ・)ノ Selphy (ノ・ェ・)ノ", Type: CommandTypeFullMatch, Cooldown: 30}) registerCommand(Command{Trigger: "!heil", Output: "(ノ・ェ・)ノ Selphy (ノ・ェ・)ノ", Type: CommandTypeFullMatch, Cooldown: 30})
registerCommand(Command{Trigger: "ayy", Output: "lmao", Type: CommandTypeFullMatch, Cooldown: 0}) registerCommand(Command{Trigger: "ayy", Output: "lmao", Type: CommandTypeFullMatch, Cooldown: 0})
registerCommand(Command{Trigger: "<:selphyPadoru:525766188240732180>", Output: "<:selphypadoru:526644596558659594> Padoru <:selphypadoru:526644596558659594> Padoru <:selphypadoru:526644596558659594> Pado Padoru <:selphypadoru:526644596558659594>", Type: CommandTypeFullMatch, Cooldown: 10800}) registerCommand(Command{Trigger: "<:selphyDango:531594585424527370>", Output: "<:dango:430669469799677953> :notes: Dango Daikazoku :notes: <:dango:430669469799677953>", Type: CommandTypeFullMatch, Cooldown: 10800})
registerCommand(Command{Trigger: "<:selphyDango:507979223244341267>", Output: "<:dango:430669469799677953> :notes: Dango Daikazoku :notes: <:dango:430669469799677953>", Type: CommandTypeFullMatch, Cooldown: 10800}) registerCommand(Command{Trigger: "praise the sun", Output: "If only I could be so grossly incandescent \\\\[T]/", Type: CommandTypeContains, IgnoreCase: true, Cooldown: 85600})
registerCommand(Command{Trigger: "praise the sun", Output: "If only I could be so grossly incandescent \\\\[T]/", Type: CommandTypeContains, IgnoreCase: true, Cooldown: 85600})
// Information // Information
registerCommand(Command{Trigger: "!welcome", OutputEmbed: getWelcomeEmbed(), Type: CommandTypeFullMatch, DMOnly: true}) registerCommand(Command{Trigger: "!welcome", OutputEmbed: getWelcomeEmbed(), Type: CommandTypeFullMatch, DMOnly: true})
registerCommand(Command{Trigger: "!mods", Output: "Bei Fragen, Problemen und Beschwerden wende dich bitte an die Moderatoren oder schick mir eine Nachricht beginnend mit !complain, um dich anonym zu beschweren.\nAktuell anwesende Mods werden dir rechts mit dem Rang „Maid“ angezeigt.", Type: CommandTypeFullMatch}) registerCommand(Command{Trigger: "!mods", Output: "Bei Fragen, Problemen und Beschwerden wende dich bitte an die Moderatoren oder schick mir eine Nachricht beginnend mit !complain, um dich anonym zu beschweren.\nAktuell anwesende Mods werden dir rechts mit dem Rang „Maid“ angezeigt.", Type: CommandTypeFullMatch})
// Admin and/or debug // Admin and/or debug
registerCommand(Command{Trigger: "<@%s> <3", Output: "<@%s> <3", Type: CommandTypeFullMatch, AdminOnly: true, OutputIsReply: true, RequiresMention: true}) registerCommand(Command{Trigger: "<@%s> <3", Output: "<@%s> <3", Type: CommandTypeFullMatch, AdminOnly: true, OutputIsReply: true, RequiresMention: true})
registerCommand(Command{Trigger: "echo", Type: CommandTypePrefix, Function: echoMessage, AdminOnly: true}) registerCommand(Command{Trigger: "echo", Type: CommandTypePrefix, Function: echoMessage, AdminOnly: true})
// This needs to be the last command because getHelpEmbed is evaluated here once, not on every function call. Putting it too early will result in missing commands in the output. // This needs to be the last command because getHelpEmbed is evaluated here once, not on every function call. Putting it too early will result in missing commands in the output.
registerCommand(Command{Trigger: "!help", OutputEmbed: getHelpEmbed(), Type: CommandTypeFullMatch}) registerCommand(Command{Trigger: "!help", OutputEmbed: getHelpEmbed(), Type: CommandTypeFullMatch})
fmt.Printf("Successfully initialized %d commands\n", len(commands)) fmt.Printf("Successfully initialized %d commands\n", len(commands))
log.Printf("Successfully initialized %d commands", len(commands)) log.Printf("Successfully initialized %d commands", len(commands))
} }

View File

@ -1,29 +1,29 @@
package main package main
import ( import (
"github.com/bwmarrin/discordgo" "fmt"
"fmt" "github.com/bwmarrin/discordgo"
) )
func getWelcomeEmbed() *discordgo.MessageEmbed { func getWelcomeEmbed() *discordgo.MessageEmbed {
return &discordgo.MessageEmbed { return &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{}, Author: &discordgo.MessageEmbedAuthor{},
Color: 0xffb90f, Color: 0xffb90f,
Description: config.WelcomeEmbed.Message, Description: config.WelcomeEmbed.Message,
Fields: []*discordgo.MessageEmbedField { Fields: []*discordgo.MessageEmbedField{
&discordgo.MessageEmbedField { &discordgo.MessageEmbedField{
Name: config.WelcomeEmbed.QuestionsTitle, Name: config.WelcomeEmbed.QuestionsTitle,
Value: config.WelcomeEmbed.QuestionsText, Value: config.WelcomeEmbed.QuestionsText,
Inline: true, Inline: true,
}, },
&discordgo.MessageEmbedField { &discordgo.MessageEmbedField{
Name: config.WelcomeEmbed.BugsTitle, Name: config.WelcomeEmbed.BugsTitle,
Value: fmt.Sprintf(config.WelcomeEmbed.BugsText, config.Admins[0]), Value: fmt.Sprintf(config.WelcomeEmbed.BugsText, config.Admins[0]),
Inline: true, Inline: true,
}, },
}, },
Thumbnail: &discordgo.MessageEmbedThumbnail{ Thumbnail: &discordgo.MessageEmbedThumbnail{
URL: config.WelcomeEmbed.Image, URL: config.WelcomeEmbed.Image,
}, },
} }
} }