Compare commits

..

1 Commits

Author SHA1 Message Date
kageru ce83264a39 cleanup and fluff 2018-07-26 18:47:55 +02:00
10 changed files with 202 additions and 648 deletions

View File

@ -1,18 +1,6 @@
# Selphybot Changelog
Updates are listed in reverse chronological order.
### 1.5 (dev)
- added !vc to create temporary voice channels
### 1.4
- a copy of each deleted message is now send via DM to the author (suggested by CommanderLook)
- seasonal fluff
- finally use gofmt
- better logging of own messages and embeds
- logging always occurs after the action that is being logged
- redirect function is much cleaner now
- commands can be limited to certain channels
### 1.3
- use global array of pointers to commands to allow easier modification and avoid unnecessary memcpy
- added feedback to !complain (used to work but was forgotten when refactoring)

View File

@ -1,3 +0,0 @@
#!/bin/sh
go build -o selphybot *.go

View File

@ -1,333 +1,156 @@
package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/deckarep/golang-set"
"log"
"regexp"
"strings"
"time"
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/deckarep/golang-set"
"strings"
"time"
"log"
"regexp"
)
type CommandType int
// These are used to specify Command.CommandType when registering new commands
const (
CommandTypePrefix CommandType = 0
CommandTypeFullMatch CommandType = 1
CommandTypeRegex CommandType = 2
CommandTypeContains CommandType = 3
CommandTypePrefix CommandType = 0
CommandTypeFullMatch CommandType = 1
CommandTypeRegex CommandType = 2
CommandTypeContains CommandType = 3
)
/*
This struct represents a command object.
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.
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)
*/
/*
This struct represents a command object.
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.
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)
*/
type Command struct {
Trigger string // must be specified
Output string // no output if unspecified
OutputEmbed *discordgo.MessageEmbed // no embed output if unspecified
Type CommandType // defaults to Prefix
Cooldown int // defaults to 0 (no cooldown)
OutputIsReply bool
RequiresMention bool
DeleteInput bool
DMOnly bool
AdminOnly bool
IgnoreCase bool
// for custom commands that go beyond prints and deletions
Function func(*discordgo.Session, *discordgo.MessageCreate)
AllowedChannels mapset.Set // allowed everywhere if blank
Trigger string // must be specified
Output string // no output if unspecified
OutputEmbed *discordgo.MessageEmbed // no embed output if unspecified
Type CommandType // defaults to Prefix
Cooldown int // defaults to 0 (no cooldown)
OutputIsReply bool
RequiresMention bool
DeleteInput bool
DMOnly bool
AdminOnly bool
IgnoreCase bool
// for custom commands that go beyond prints and deletions
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
func registerCommand(command Command) {
if command.Trigger == "" {
fmt.Println("Cannot register a command with no trigger. Skipping.")
return
}
if command.IgnoreCase {
command.Trigger = strings.ToLower(command.Trigger)
}
command.UsersOnCooldown = mapset.NewSet()
if command.AllowedChannels == nil {
command.AllowedChannels = mapset.NewSet()
}
commands = append(commands, &command)
if command.Trigger == "" {
fmt.Println("Cannot register a command with no trigger. Skipping.")
return
}
if command.IgnoreCase {
command.Trigger = strings.ToLower(command.Trigger)
}
command.UsersOnCooldown = mapset.NewSet()
commands = append(commands, &command)
}
/*
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.
If one of the commands matches, execute that command and return.
Only one command can be executed per message. Earlier defined commands take precedence.
This is a deliberate choice (for now).
*/
func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
// Properly log embeds
if len(m.Embeds) > 0 {
log.Printf("<Self> %s", m.Embeds[0].Description)
} else {
log.Printf("<Self> %s", m.Content)
}
return
}
for _, command := range commands {
content := m.Content
if command.IgnoreCase {
content = strings.ToLower(content)
}
if command.RequiresMention {
command.Trigger = fmt.Sprintf(command.Trigger, s.State.User.ID)
}
switch command.Type {
case CommandTypePrefix:
if strings.HasPrefix(content, command.Trigger) {
executeCommand(s, m, command)
return
}
case CommandTypeFullMatch:
if content == command.Trigger {
executeCommand(s, m, command)
return
}
case CommandTypeRegex:
match, _ := regexp.MatchString(command.Trigger, content)
if match {
executeCommand(s, m, command)
return
}
case CommandTypeContains:
if strings.Contains(content, command.Trigger) {
executeCommand(s, m, command)
return
}
}
}
}
/*
Executes the given command on the given message and session.
Sets command cooldowns if necessary and also clears them again.
*/
func executeCommand(session *discordgo.Session, message *discordgo.MessageCreate, command *Command) {
if commandAllowed(session, message, command) {
log.Printf("Executed command %s triggered by user %s", command.Trigger, userToString(message.Author))
if command.Cooldown > 0 && !isDM(session, message) && !isAdmin(message.Author) {
command.UsersOnCooldown.Add(message.Author.ID)
go removeCooldown(command, message.Author.ID)
}
if command.Function == nil {
// simple reply
if command.OutputEmbed == nil {
messageContent := generateReply(message, command)
session.ChannelMessageSend(message.ChannelID, messageContent)
} else {
session.ChannelMessageSendEmbed(message.ChannelID, command.OutputEmbed)
}
if command.DeleteInput {
deleteAndSendViaDM(session, message)
}
} else {
// execute custom function
command.Function(session, message)
}
} else {
log.Printf("Denied command %s to user %s.", command.Trigger, userToString(message.Author))
}
}
/*
* Check if a user has the permission to trigger a given command.
* To be honest, this whole logic is a mess, but I dont know a better way to handle it.
/*
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.
If one of the commands matches, execute that command and return.
Only one command can be executed per message. Earlier defined commands take precedence.
This is a deliberate choice (for now).
*/
func commandAllowed(session *discordgo.Session, message *discordgo.MessageCreate, command *Command) bool {
// no restrictions for admins
if isAdmin(message.Author) {
return true
}
// blacklist admin commands for everyone else
if command.AdminOnly {
return false
}
// cooldowns are irrelevant in DMs
if !isDM(session, message) && command.UsersOnCooldown.Contains(message.Author.ID) {
return false
}
// the command is not limited to DMs or we are inside a DM chat
if command.DMOnly && !isDM(session, message) {
return false
}
// no allowed channels = all channels are allowed.
// DMs are whitelisted by default
if command.AllowedChannels.Cardinality() != 0 &&
!command.AllowedChannels.Contains(message.ChannelID) &&
isDM(session, message) {
return false
}
return true
func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
log.Printf("<Self> %s", m.Content)
return
}
for _, command := range commands {
content := m.Content
if command.IgnoreCase {
content = strings.ToLower(content)
}
if command.RequiresMention {
command.Trigger = fmt.Sprintf(command.Trigger, s.State.User.ID)
}
switch command.Type {
case CommandTypePrefix:
if strings.HasPrefix(content, command.Trigger) {
executeCommand(s, m, command)
return
}
case CommandTypeFullMatch:
if content == command.Trigger {
executeCommand(s, m, command)
return
}
case CommandTypeRegex:
match, _ := regexp.MatchString(command.Trigger, content)
if match {
executeCommand(s, m, command)
return
}
case CommandTypeContains:
if strings.Contains(content, command.Trigger) {
executeCommand(s, m, command)
return
}
}
}
}
/*
Executes the given command on the given message and session.
Sets command cooldowns if necessary and also clears them again.
*/
func executeCommand(session *discordgo.Session, message *discordgo.MessageCreate, command *Command) {
if isAdmin(message.Author) || // no restrictions for admins
(!command.AdminOnly && (isDM(session, message) || !command.UsersOnCooldown.Contains(message.Author.ID)) &&
(!command.DMOnly || isDM(session, message))) {
log.Printf("Executed command %s triggered by user %s", command.Trigger, userToString(message.Author))
if command.Cooldown > 0 && !isDM(session, message) && !isAdmin(message.Author) {
command.UsersOnCooldown.Add(message.Author.ID)
go removeCooldown(command, message.Author.ID)
}
if command.Function == nil {
// simple reply
if command.OutputEmbed == nil {
messageContent := generateReply(message, command)
session.ChannelMessageSend(message.ChannelID, messageContent)
} else {
session.ChannelMessageSendEmbed(message.ChannelID, command.OutputEmbed)
}
if command.DeleteInput {
session.ChannelMessageDelete(message.ChannelID, message.ID)
}
} else {
// execute custom function
command.Function(session, message)
}
} else {
log.Printf("Denied command %s to user %s.", command.Trigger, userToString(message.Author))
}
}
func removeCooldown(command *Command, uid string) {
time.Sleep(time.Duration(command.Cooldown) * time.Second)
if command.UsersOnCooldown.Contains(uid) {
command.UsersOnCooldown.Remove(uid)
}
}
func deleteAndSendViaDM(s *discordgo.Session, message *discordgo.MessageCreate) {
s.ChannelMessageDelete(message.ChannelID, message.ID)
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, message.Content)
time.Sleep(time.Duration(command.Cooldown) * time.Second)
if command.UsersOnCooldown.Contains(uid) {
command.UsersOnCooldown.Remove(uid)
}
}
func generateReply(message *discordgo.MessageCreate, command *Command) string {
output := command.Output
if command.OutputIsReply {
output = fmt.Sprintf(output, message.Author.ID)
}
return output
output := command.Output
if command.OutputIsReply {
output = fmt.Sprintf(output, message.Author.ID)
}
return output
}
/*
* Im beginning to doubt my own self-imposed limitations of
* only allowing func(session, message) to be attached to commands,
* but refactoring that might be more effort than its worth.
* Hence, small wrappers around the redirect function.
*/
func modComplain(s *discordgo.Session, m *discordgo.MessageCreate) {
success := redirectMessage(s, m, config.ModChannel, true)
dm, _ := s.UserChannelCreate(m.Author.ID)
if success {
s.ChannelMessageSend(dm.ID, config.ComplaintReceivedMessage)
} else {
s.ChannelMessageSend(dm.ID, "Could not send message. Please tell kageru about this.")
}
}
func selphyComplain(s *discordgo.Session, m *discordgo.MessageCreate) {
dm_target, _ := s.UserChannelCreate("190958368301645824")
success := redirectMessage(s, m, dm_target.ID, true)
dm, _ := s.UserChannelCreate(m.Author.ID)
if success {
s.ChannelMessageSend(dm.ID, config.ComplaintReceivedMessage)
} else {
s.ChannelMessageSend(dm.ID, "Could not send message. Please tell kageru about this.")
}
}
func redirectMessage(s *discordgo.Session, m *discordgo.MessageCreate, target string, isEmbed bool) bool {
var err error
if isEmbed {
embed := &discordgo.MessageEmbed{
// Embed are anonymized by default for now. Fits the use case.
Author: &discordgo.MessageEmbedAuthor{},
Color: 0xbb0000,
Description: m.Content,
}
_, err = s.ChannelMessageSendEmbed(target, embed)
} else {
_, err = s.ChannelMessageSend(target, messageToString(m.Message))
}
if err != nil {
log.Printf("Could not redirect message", err)
return false
}
return true
}
func echoMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
s.ChannelMessageSend(m.ChannelID, m.Content)
}
func giveAgeRole(s *discordgo.Session, m *discordgo.MessageCreate) {
Member, err := s.GuildMember(config.ServerID, m.Author.ID)
if err != nil {
log.Printf("User could not be retrieved for role assignment\n%s", err)
return
}
dm, err := s.UserChannelCreate(Member.User.ID)
if err != nil {
log.Printf("Could not reply to user\n%s", err)
return
}
required := mapset.NewSetWith("416184227672096780", "416184208470310922", "416184150404628480", "416184132473847810", "440996904948465664")
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 {
// check if the user has a twitch sub or any of the patreon roles
isAllowed := false
for _, curRole := range Member.Roles {
if required.Contains(curRole) {
isAllowed = true
}
}
if !isAllowed {
s.ChannelMessageSend(dm.ID, "Du kannst dir keine Rolle zuweisen, da weder Patreon noch Twitch mit deinem Account verlinkt ist oder du Selphy auf keiner dieser Plattformen unterstützt. Bei Problemen wende dich bitte an die `!mods`.")
log.Printf("Denied role %s to %s. User is neither patron nor sub.", roleName(s.State, newRole), userToString(m.Author))
return
}
for _, curRole := range Member.Roles {
// If the user already has one of the available roles, tell them 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
}
}
}
s.GuildMemberRoleAdd(config.ServerID, m.Author.ID, role)
s.ChannelMessageSend(dm.ID, "Haaai, Ryoukai desu~")
log.Printf("Giving Role %s to %s", roleName(s.State, role), userToString(m.Author))
}
}
}
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
}

View File

@ -1,38 +1,21 @@
package main
import (
"encoding/json"
"os"
"os"
"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
Admins []string
ServerID string
Token string
}
func readConfig() Config {
file, _ := os.Open("config.json")
conf := Config{}
json.NewDecoder(file).Decode(&conf)
file.Close()
return conf
file, _ := os.Open("config.json")
conf := Config{}
json.NewDecoder(file).Decode(&conf)
file.Close()
return conf
}

View File

@ -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"
}

View File

@ -1,53 +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 @%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. https://cdn.discordapp.com/attachments/333450842151714816/591581555219234826/Selphy-queen.png", 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)
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
}
}
}
s.ChannelMessageSend(dm.ID, "Haaai, Ryoukai desu~")
s.GuildMemberRoleAdd(config.ServerID, m.Author.ID, role)
log.Printf("Giving Role %s to %s", roleName(s.State, role), userToString(m.Author))
}
}
}

View File

@ -1,67 +1,46 @@
package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"log"
"github.com/bwmarrin/discordgo"
"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)
return fmt.Sprintf("%s#%s (ID: %s)", u.Username, u.Discriminator, u.ID)
}
func roleName(s *discordgo.State, rid string) string {
role, _ := s.Role(config.ServerID, rid)
return role.Name
role, _ := s.Role(config.ServerID, rid)
return role.Name
}
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 {
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 {
channel, _ := s.Channel(cid)
return channel
channel, _ := s.Channel(cid)
return channel
}
func getUser(s *discordgo.Session, uid string) *discordgo.User {
user, _ := s.User(uid)
return user
user, _ := s.User(uid)
return user
}
func isDM(s *discordgo.Session, m *discordgo.MessageCreate) bool {
return (getChannel(s.State, m.ChannelID).Type == discordgo.ChannelTypeDM)
}
func getDMChannelFromMessage(s *discordgo.Session, m *discordgo.MessageCreate) *discordgo.Channel {
dm, _ := s.UserChannelCreate(m.Author.ID)
return dm
return (getChannel(s.State, m.ChannelID).Type == discordgo.ChannelTypeDM)
}
func isAdmin(u *discordgo.User) bool {
for _, admin := range config.Admins {
if u.ID == admin {
return true
}
}
return false
}
func getServer() (*discordgo.Guild, error) {
server, err := state.Guild(config.ServerID)
return server, err
}
func remove(channels []*discordgo.Channel, position int) []*discordgo.Channel {
channels[len(channels)-1], channels[position] = channels[position], channels[len(channels)-1]
return channels[:len(channels)-1]
for _, admin := range config.Admins {
if u.ID == admin {
return true
}
}
return false
}

126
main.go
View File

@ -1,103 +1,63 @@
package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/deckarep/golang-set"
"log"
"os"
"os/signal"
"syscall"
"fmt"
"os/signal"
"os"
"syscall"
"log"
"github.com/bwmarrin/discordgo"
)
var config = readConfig()
var commands []*Command
var state *discordgo.State
func main() {
session, err := discordgo.New("Bot " + config.Token)
if err != nil {
fmt.Println("error: ", err)
return
}
defer session.Close()
dg, err := discordgo.New("Bot " + config.Token)
if err != nil {
fmt.Println("error: ", err)
return
}
defer dg.Close()
session.AddHandler(evaluateMessage)
session.AddHandler(onJoin)
err = session.Open()
if err != nil {
fmt.Println("No connection:\n", err)
return
}
dg.AddHandler(evaluateMessage)
dg.AddHandler()
err = dg.Open()
if err != nil {
fmt.Println("No connection:\n", err)
return
}
f, err := os.OpenFile("selphybot.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Println("Error opening log file:\n", err)
}
defer f.Close()
log.SetOutput(f)
session.UpdateStatus(0, "!help")
state = discordgo.NewState()
server, err := session.Guild(config.ServerID)
if err != nil {
fmt.Println("Guild incorrectly configured. Exiting...")
return
}
state.GuildAdd(server)
go checkAndDeleteUnusedChannels(session)
addCommands()
f, err := os.OpenFile("selphybot.log", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666)
if err != nil {
fmt.Println("Error opening log file:\n", err)
}
defer f.Close()
log.SetOutput(f)
dg.UpdateStatus(0, "!help")
addCommands()
fmt.Println("Bot running. selphyWoo")
log.Println("Bot running. selphyWoo")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
fmt.Println("Bot running. selphyWoo")
log.Println("Bot running. selphyWoo")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
for _, channel := range tempChannels {
session.ChannelDelete(channel.ID)
}
fmt.Println("Exiting...")
log.Println("Exiting...")
fmt.Println("Exiting...")
log.Println("Exiting...")
}
// I’ll just put all of the commands here for now.
func addCommands() {
// Moderation
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: "einzigste", Output: "<@%s> Es heißt „einzige“, du Tuba.", DeleteInput: true, OutputIsReply: true, Type: CommandTypeContains})
registerCommand(Command{Trigger: "!complain", Type: CommandTypePrefix, DMOnly: true, Function: modComplain})
registerCommand(Command{Trigger: "!scomplain", Type: CommandTypePrefix, DMOnly: true, Function: selphyComplain})
registerCommand(Command{Trigger: "!beschwerde", Type: CommandTypePrefix, DMOnly: true, Function: modComplain})
// Moderation
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})
for comm, _ := range config.RoleCommands {
registerCommand(Command{Trigger: comm, Type: CommandTypeFullMatch, DMOnly: true, Function: giveAgeRole})
}
// 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})
// Fluff
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:531594585424527370>", 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})
// Features :Pog:
registerCommand(Command{Trigger: "!vc ", Type: CommandTypePrefix, Function: parseVoiceChannelCommand, AllowedChannels: mapset.NewSetWith("525852491976278016")})
// 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})
fmt.Printf("Successfully initialized %d commands\n", len(commands))
log.Printf("Successfully initialized %d commands", len(commands))
fmt.Printf("Successfully initialized %d commands\n", len(commands))
log.Printf("Successfully initialized %d commands", len(commands))
}

View File

@ -1,74 +0,0 @@
package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"log"
"strconv"
"strings"
"time"
)
var tempChannels []*discordgo.Channel
func parseVoiceChannelCommand(session *discordgo.Session, message *discordgo.MessageCreate) {
userLimit, err := strconv.Atoi(strings.Split(message.Content, " ")[1])
if err != nil {
session.ChannelMessageSend(message.ChannelID, "Error: Expected a number after !vc")
log.Printf("Incorrect syntax for !vc, “%s” triggered by %s", message.Content, userToString(message.Author))
return
}
if userLimit > 99 {
session.ChannelMessageSend(message.ChannelID, fmt.Sprintf("Als ob %d Leute *mit dir* in einen Channel wollen.", userLimit-1))
log.Printf("%s tried to create a channel with %d slots", userToString(message.Author), userLimit)
return
}
createData := discordgo.GuildChannelCreateData{
Name: fmt.Sprintf("%s’s Volatile Corner", message.Author.Username),
Type: discordgo.ChannelTypeGuildVoice,
UserLimit: userLimit,
ParentID: "410162599762853909",
}
channel, err := session.GuildChannelCreateComplex(message.GuildID, createData)
if err != nil {
session.ChannelMessageSend(message.ChannelID, "Couldn’t create the voice channel. Please bug kageru about this.")
log.Printf("Failed to create voice channel, %s", err)
return
}
tempChannels = append(tempChannels, channel)
err = session.GuildMemberMove(config.ServerID, message.Author.ID, channel.ID)
if err != nil {
log.Printf("Couldn’t move user %s: %s", userToString(message.Author), err)
}
session.ChannelMessageSend(message.ChannelID, "haaaai~")
log.Printf("Created channel %s", channel.ID)
}
func checkAndDeleteUnusedChannels(session *discordgo.Session) {
for true {
time.Sleep(30 * time.Second)
server, err := getServer()
if err == nil {
for i, channel := range tempChannels {
if channelIsEmpty(channel.ID, server.VoiceStates) {
session.ChannelDelete(channel.ID)
tempChannels = remove(tempChannels, i)
log.Printf("Deleted channel %s", channel.ID)
log.Printf("Tempchannels: %d", len(tempChannels))
break
}
}
} else {
log.Printf("Could not retrieve voice state from API, %s", err)
}
}
}
func channelIsEmpty(channelID string, voiceStates []*discordgo.VoiceState) bool {
for _, state := range voiceStates {
if channelID == state.ChannelID {
return false
}
}
return true
}

View File

@ -1,29 +0,0 @@
package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
)
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,
},
}
}