cleanup and fluff

This commit is contained in:
kageru 2018-07-26 18:47:55 +02:00
parent d8732a06c1
commit ce83264a39
7 changed files with 6 additions and 254 deletions

@ -154,97 +154,3 @@ func generateReply(message *discordgo.MessageCreate, command *Command) string {
return output
}
/*
Any message passed to this method will be redirected to config.ModChannel.
This is useful for anonymous complaints or similar messages.
*/
func redirectComplaint(s *discordgo.Session, m *discordgo.MessageCreate) {
embed := &discordgo.MessageEmbed {
Author: &discordgo.MessageEmbedAuthor{},
Color: 0xbb0000,
Description: m.Content,
}
s.ChannelMessageSendEmbed(config.ModChannel, embed)
dm, _ := s.UserChannelCreate(m.Author.ID)
s.ChannelMessageSend(dm.ID, config.ComplaintReceivedMessage)
}
// copy paste programming btw :haHAA:
func redirectComplaintToDM(s *discordgo.Session, m *discordgo.MessageCreate) {
embed := &discordgo.MessageEmbed {
Author: &discordgo.MessageEmbedAuthor{},
Color: 0xbb0000,
Description: m.Content,
}
dm_target, _ := s.UserChannelCreate("190958368301645824")
s.ChannelMessageSendEmbed(dm_target.ID, embed)
dm, _ := s.UserChannelCreate(m.Author.ID)
s.ChannelMessageSend(dm.ID, config.ComplaintReceivedMessage)
}
func echoMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
s.ChannelMessageSend(m.ChannelID, m.Content)
}
func giveAgeRole(s *discordgo.Session, m *discordgo.MessageCreate) {
Member, _ := s.GuildMember(config.ServerID, m.Author.ID)
dm, _ := s.UserChannelCreate(Member.User.ID)
for command, role := range config.RoleCommands {
if m.Content == command {
// Found the command that was triggered
// 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.
for _, newRole := range config.RoleCommands {
for _, curRole := range Member.Roles {
// If the user already has one of the available roles, tell him and exit
if newRole == curRole {
if curRole == role {
// User is trying to get the role they already have
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))
} else {
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))
}
return
}
}
}
log.Printf("Giving Role %s to %s", roleName(s.State, role), userToString(m.Author))
s.ChannelMessageSend(dm.ID, "Haaai, Ryoukai desu~")
s.GuildMemberRoleAdd(config.ServerID, m.Author.ID, role)
}
}
}
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"
for _, command := range commands {
if command.Type != CommandTypeRegex && !command.AdminOnly && !command.DMOnly {
commandList += "- " + command.Trigger + "\n"
}
}
commandList += "```"
embed := &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{},
Color: 0xffb90f,
Description: "__Hilfe__",
Fields: []*discordgo.MessageEmbedField {
&discordgo.MessageEmbedField {
Name: "__Commands__",
Value: commandList,
Inline: true,
},
&discordgo.MessageEmbedField {
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]),
Inline: true,
},
},
Thumbnail: &discordgo.MessageEmbedThumbnail{
URL: "https://static-cdn.jtvnw.net/emoticons/v1/1068185/3.0",
},
}
return embed
}

@ -5,31 +5,12 @@ import (
"encoding/json"
)
type Embed struct {
Message string
QuestionsTitle string
QuestionsText string
BugsTitle string
BugsText string
Image string
}
type Config struct {
Admins []string
ServerID string
LockedRoleID string
Token string
WelcomeChannel string
GeneralChannel string
SendWelcomeDM bool
RequireAccept bool
ComplaintReceivedMessage string
ModChannel string
WelcomeEmbed Embed
RoleCommands map[string]string
}
func readConfig() Config {
file, _ := os.Open("config.json")
conf := Config{}

@ -1,28 +1,8 @@
{
"Token": "your login token",
"Token": "token",
"Admins": [
"your user ID",
"another admin ID"
],
"ServerID": "the server the bot will be running on",
"RequireAccept": false,
"LockedRoleID": "the role to be given to locked users (readme)",
"WelcomeChannel": "channel for welcome messages",
"GeneralChannel": "channel to @mention users who disabled their DMs",
"SendWelcomeDM": true,
"ModChannel": "channel for anonymous complaints",
"ComplaintReceivedMessage": "Complaint received.",
"RoleCommands": {
"!u18": "role id for users under 18",
"!18+": "role over 18",
"!18": "aliases are possible, so this could be the same role as above"
},
"WelcomeEmbed": {
"Message": "Welcome, new user. this message will be part of an embed",
"QuestionsTitle": "__Questions__",
"QuestionsText": "pls ask the mods. kthxbye",
"BugsTitle": "__Bugs__",
"BugsText": "please ask <@%s>. (this will be formatted with Admins[0] above",
"Image": "https://static-cdn.jtvnw.net/emoticons/v1/970966/3.0"
}
"ServerID": "356414885292277771"
}

@ -1,55 +0,0 @@
package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"log"
)
func onJoin(s *discordgo.Session, member *discordgo.GuildMemberAdd) {
if !member.User.Bot && config.RequireAccept {
s.GuildMemberRoleAdd(config.ServerID, member.User.ID, config.LockedRoleID)
}
if !member.User.Bot && config.SendWelcomeDM {
dm, err := s.UserChannelCreate(member.User.ID)
if err != nil {
log.Println(fmt.Sprintf("Error creating DM with %s", userToString(member.User), err))
} else {
embed := getWelcomeEmbed()
_, err = s.ChannelMessageSendEmbed(dm.ID, embed)
if err != nil {
log.Println(fmt.Sprintf("Error sending DM to %s", userToString(member.User), err))
}
}
if err != nil {
// if any of the preceding operations produced an error
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))
}
}
log.Printf("User joined: %s", userToString(member.User))
}
func onDM(s *discordgo.Session, m *discordgo.MessageCreate) {
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)
Member, _ := s.GuildMember(config.ServerID, m.Author.ID)
dm, _ := s.UserChannelCreate(Member.User.ID)
for comm, role := range config.RoleCommands {
if m.Content == comm {
for _, irole := range config.RoleCommands {
for _, mrole := range Member.Roles {
if irole == mrole {
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))
return
}
}
}
log.Printf("Giving Role %s to %s", roleName(s.State, role), userToString(m.Author))
s.ChannelMessageSend(dm.ID, "Haaai, Ryoukai desu~")
s.GuildMemberRoleAdd(config.ServerID, m.Author.ID, role)
}
}
}

@ -2,15 +2,9 @@ package main
import (
"github.com/bwmarrin/discordgo"
"log"
"fmt"
)
func unlockUser(s *discordgo.Session, id string) {
s.GuildMemberRoleRemove(config.ServerID, id, config.LockedRoleID)
log.Printf("Removed lock from user: %s", userToString(getUser(s, id)))
}
func userToString(u *discordgo.User) string {
return fmt.Sprintf("%s#%s (ID: %s)", u.Username, u.Discriminator, u.ID)
}

33
main.go

@ -21,7 +21,7 @@ func main() {
defer dg.Close()
dg.AddHandler(evaluateMessage)
dg.AddHandler(onJoin)
dg.AddHandler()
err = dg.Open()
if err != nil {
fmt.Println("No connection:\n", err)
@ -50,37 +50,12 @@ func main() {
// I’ll just put all of the commands here for now.
func addCommands() {
// Moderation
registerCommand(Command{Trigger: "^[^`]*([()|DoOvVcC][-=^']?;|;[-=^']?[()|DoOpPvVcC]|: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: "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: "!scomplain", Type: CommandTypePrefix, DMOnly: true, Function: redirectComplaintToDM})
registerCommand(Command{Trigger: "!beschwerde", Type: CommandTypePrefix, DMOnly: true, Function: redirectComplaint})
for comm, _ := range config.RoleCommands {
registerCommand(Command{Trigger: comm, Type: CommandTypeFullMatch, DMOnly: true, Function: giveAgeRole})
}
// 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: "!heil", Output: "(ノ・ェ・)ノ Selphy (ノ・ェ・)ノ", Type: CommandTypeFullMatch, Cooldown: 30})
registerCommand(Command{Trigger: "ayy", Output: "lmao", Type: CommandTypeFullMatch, Cooldown: 0})
registerCommand(Command{Trigger: "<:selphyDango:441001954542616576>", 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})
// Information
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})
// Admin and/or debug
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})
// 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})
// Misc
registerCommand(Command{Trigger: "<@%s> <3", Output: "<@%s> <3", Type: CommandTypeFullMatch, AdminOnly: false, OutputIsReply: true, RequiresMention: true})
registerCommand(Command{Trigger: "ist flauschig", Output: "Du bist flauschig! <3", Type: CommandTypeContains})
fmt.Printf("Successfully initialized %d commands\n", len(commands))
log.Printf("Successfully initialized %d commands", len(commands))

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