diff --git a/cmd/welcomebot/config/config.go b/cmd/welcomebot/config/config.go new file mode 100644 index 0000000..123af9a --- /dev/null +++ b/cmd/welcomebot/config/config.go @@ -0,0 +1,19 @@ +package config + +import ( + "github.com/disgoorg/snowflake/v2" + "os" +) + +var ( + Token = os.Getenv("disgo_token") + RegisterGuildID = snowflake.GetEnv("disgo_guild_id") +) + +var ( + ChannelRulesMention string + ChannelInfoMention string + ChannelWelcomeID snowflake.ID + RoleAppleID snowflake.ID + RoleAppleCoreID snowflake.ID +) diff --git a/cmd/welcomebot/listener/listener.go b/cmd/welcomebot/listener/listener.go new file mode 100644 index 0000000..9111273 --- /dev/null +++ b/cmd/welcomebot/listener/listener.go @@ -0,0 +1,155 @@ +package listener + +import ( + "fmt" + "github.com/disgoorg/disgo/discord" + "github.com/disgoorg/disgo/events" + "grow.rievo.dev/discordBots/cmd/welcomebot/config" + "time" +) + +func JoinListener(event *events.GuildMemberJoin) { + // on join send message in welcome + user := event.Member.User + if event.GuildID.String() != config.RegisterGuildID.String() { + event.Client().Logger().Error("join from an other guild") + return + } + guild, err := event.Client().Rest().GetGuild(config.RegisterGuildID, false) + if err != nil { + event.Client().Logger().Error("could not find guild: ", err) + return + } + + _, err = event.Client().Rest().CreateMessage(config.ChannelWelcomeID, + discord.NewMessageCreateBuilder(). + SetContent(user.Mention()). + SetEphemeral(false). + AddActionRow(discord.NewSecondaryButton("approve", "approve")). + SetEmbeds(discord.NewEmbedBuilder(). + SetColor(16068661). + SetTimestamp(time.Now()). + SetThumbnail(user.EffectiveAvatarURL(discord.WithSize(1024))). + AddField( + fmt.Sprintf("Welcome to the %v Server", guild.Name), + fmt.Sprintf("Welcome %v (`%v`) on %v 🍎", user.Mention(), user.Username, guild.Name), false). + AddField("Next Step", fmt.Sprintf("schau dir die %v und spaeter die %v an", config.ChannelRulesMention, config.ChannelInfoMention), false). + Build()). + Build(), + ) + if err != nil { + event.Client().Logger().Error("on sending join message: ", err) + return + } +} + +func LeaveListener(event *events.GuildMemberLeave) { + user := event.User + + if event.GuildID.String() != config.RegisterGuildID.String() { + event.Client().Logger().Error("leave from an other guild") + return + } + guild, err := event.Client().Rest().GetGuild(config.RegisterGuildID, false) + if err != nil { + event.Client().Logger().Error("could not find guild: ", err) + return + } + + _, err = event.Client().Rest().CreateMessage(config.ChannelWelcomeID, + discord.NewMessageCreateBuilder(). + SetContent( + fmt.Sprintf("%v (`%v`) left %v", user.Mention(), user.Username, guild.Name)). + Build(), + ) + if err != nil { + event.Client().Logger().Error("on sending leave message: ", err) + return + } +} + +func ReactionListener(event *events.ComponentInteractionCreate) { + + if event.ChannelID() != config.ChannelWelcomeID { + // reaction is not in welcome chat + return + } + + approverID := event.User().ID + if approverID.String() == event.Client().ID().String() { + // reaction user is bot itself + return + } + if event.ButtonInteractionData().CustomID() != "approve" { + // not approve button + return + } + approver, err := event.Client().Rest().GetMember(config.RegisterGuildID, approverID) + if err != nil { + event.Client().Logger().Error("could not find user: ", err) + return + } + hasPermission := func() bool { + for _, roleID := range approver.RoleIDs { + if roleID.String() == config.RoleAppleCoreID.String() { + return true + } + } + return false + }() + + if hasPermission == false { + err = event.CreateMessage(discord.NewMessageCreateBuilder(). + SetEphemeral(true). + SetContent("you don't have permissions to do this"). + Build()) + if err != nil { + event.Client().Logger().Error("could not respond to reaction: ", err) + return + } + return + } + + messageID := event.Message.ID + message, err := event.Client().Rest().GetMessage(config.ChannelWelcomeID, messageID) + if err != nil { + event.Client().Logger().Error("could not find message: ", err) + return + } + if len(message.Mentions) != 1 { + // there should be only one mention in the message + return + } + user := message.Mentions[0] + + // add role to user (bot role needs to be above roleAppleID) + err = event.Client().Rest().AddMemberRole(config.RegisterGuildID, user.ID, config.RoleAppleID) + if err != nil { + event.Client().Logger().Error("not able to add role: ", err) + return + } + + // update embed (and time) + if len(message.Embeds) != 1 { + // there should be only one embed in the message + return + } + embed := message.Embeds[0] + updatedMessage := discord.NewEmbedBuilder(). + SetColor(16068661). + SetTimestamp(time.Now()). + SetThumbnail(embed.Thumbnail.URL). + AddFields(embed.Fields...). + AddField("Approval", fmt.Sprintf("%v approved by %v (`%v`)", user.Mention(), approver.Mention(), approver.User.Username), false). + Build() + + err = event.UpdateMessage(discord.NewMessageUpdateBuilder(). + SetContent(message.Content). + SetEmbeds(updatedMessage). + ClearContainerComponents(). + Build()) + if err != nil { + event.Client().Logger().Error("could not update message: ", err) + return + } +} diff --git a/cmd/welcomebot/main.go b/cmd/welcomebot/main.go index d6f4374..3639b32 100644 --- a/cmd/welcomebot/main.go +++ b/cmd/welcomebot/main.go @@ -2,33 +2,16 @@ package main import ( "context" - "fmt" - "os" - "os/signal" - "syscall" - "time" - "github.com/disgoorg/disgo" "github.com/disgoorg/disgo/bot" "github.com/disgoorg/disgo/cache" - "github.com/disgoorg/disgo/discord" - "github.com/disgoorg/disgo/events" "github.com/disgoorg/disgo/gateway" "github.com/disgoorg/log" - "github.com/disgoorg/snowflake/v2" -) - -var ( - token = os.Getenv("disgo_token") - registerGuildID = snowflake.GetEnv("disgo_guild_id") -) - -var ( - channelRulesMention string - channelInfoMention string - channelWelcomeID snowflake.ID - roleAppleID snowflake.ID - roleAppleCoreID snowflake.ID + "grow.rievo.dev/discordBots/cmd/welcomebot/config" + "grow.rievo.dev/discordBots/cmd/welcomebot/listener" + "os" + "os/signal" + "syscall" ) func main() { @@ -38,7 +21,7 @@ func main() { // permissions: Manage Roles // intent: Server Members Intent - client, err := disgo.New(token, + client, err := disgo.New(config.Token, bot.WithGatewayConfigOpts( gateway.WithIntents(gateway.IntentGuildMembers), ), @@ -47,9 +30,9 @@ func main() { cache.FlagsNone, ), ), - bot.WithEventListenerFunc(joinListener), - bot.WithEventListenerFunc(leaveListener), - bot.WithEventListenerFunc(reactionListener), + bot.WithEventListenerFunc(listener.JoinListener), + bot.WithEventListenerFunc(listener.LeaveListener), + bot.WithEventListenerFunc(listener.ReactionListener), ) if err != nil { log.Fatal("error while building disgo instance: ", err) @@ -62,31 +45,31 @@ func main() { log.Fatal("error while connecting to gateway: ", err) } - channels, err := client.Rest().GetGuildChannels(registerGuildID) + channels, err := client.Rest().GetGuildChannels(config.RegisterGuildID) for _, channel := range channels { switch channel.Name() { case "🔨-rules": - channelRulesMention = channel.Mention() + config.ChannelRulesMention = channel.Mention() case "❕-info": - channelInfoMention = channel.Mention() + config.ChannelInfoMention = channel.Mention() case "👋-welcome": - channelWelcomeID = channel.ID() + config.ChannelWelcomeID = channel.ID() } } - if channelRulesMention == "" || channelInfoMention == "" || channelWelcomeID == 0 { + if config.ChannelRulesMention == "" || config.ChannelInfoMention == "" || config.ChannelWelcomeID == 0 { log.Fatal("couldn't find needed channel") } - roles, err := client.Rest().GetRoles(registerGuildID) + roles, err := client.Rest().GetRoles(config.RegisterGuildID) for _, role := range roles { switch role.Name { case "Apple": - roleAppleID = role.ID + config.RoleAppleID = role.ID case "Apple Core": - roleAppleCoreID = role.ID + config.RoleAppleCoreID = role.ID } } - if roleAppleID == 0 || roleAppleCoreID == 0 { + if config.RoleAppleID == 0 || config.RoleAppleCoreID == 0 { log.Fatal("couldn't find needed role") } @@ -95,149 +78,3 @@ func main() { signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) <-s } - -func joinListener(event *events.GuildMemberJoin) { - // on join send message in welcome - user := event.Member.User - if event.GuildID.String() != registerGuildID.String() { - event.Client().Logger().Error("join from an other guild") - return - } - guild, err := event.Client().Rest().GetGuild(registerGuildID, false) - if err != nil { - event.Client().Logger().Error("could not find guild: ", err) - return - } - - _, err = event.Client().Rest().CreateMessage(channelWelcomeID, - discord.NewMessageCreateBuilder(). - SetContent(user.Mention()). - SetEphemeral(false). - AddActionRow(discord.NewSecondaryButton("approve", "approve")). - SetEmbeds(discord.NewEmbedBuilder(). - SetColor(16068661). - SetTimestamp(time.Now()). - SetThumbnail(user.EffectiveAvatarURL(discord.WithSize(1024))). - AddField( - fmt.Sprintf("Welcome to the %v Server", guild.Name), - fmt.Sprintf("Welcome %v (`%v`) on %v 🍎", user.Mention(), user.Username, guild.Name), false). - AddField("Next Step", fmt.Sprintf("schau dir die %v und spaeter die %v an", channelRulesMention, channelInfoMention), false). - Build()). - Build(), - ) - if err != nil { - event.Client().Logger().Error("on sending join message: ", err) - return - } -} - -func leaveListener(event *events.GuildMemberLeave) { - user := event.User - - if event.GuildID.String() != registerGuildID.String() { - event.Client().Logger().Error("leave from an other guild") - return - } - guild, err := event.Client().Rest().GetGuild(registerGuildID, false) - if err != nil { - event.Client().Logger().Error("could not find guild: ", err) - return - } - - _, err = event.Client().Rest().CreateMessage(channelWelcomeID, - discord.NewMessageCreateBuilder(). - SetContent( - fmt.Sprintf("%v (`%v`) left %v", user.Mention(), user.Username, guild.Name)). - Build(), - ) - if err != nil { - event.Client().Logger().Error("on sending leave message: ", err) - return - } -} - -func reactionListener(event *events.ComponentInteractionCreate) { - - if event.ChannelID() != channelWelcomeID { - // reaction is not in welcome chat - return - } - - approverID := event.User().ID - if approverID.String() == event.Client().ID().String() { - // reaction user is bot itself - return - } - if event.ButtonInteractionData().CustomID() != "approve" { - // not approve button - return - } - approver, err := event.Client().Rest().GetMember(registerGuildID, approverID) - if err != nil { - event.Client().Logger().Error("could not find user: ", err) - return - } - hasPermission := func() bool { - for _, roleID := range approver.RoleIDs { - if roleID.String() == roleAppleCoreID.String() { - return true - } - } - return false - }() - - if hasPermission == false { - err = event.CreateMessage(discord.NewMessageCreateBuilder(). - SetEphemeral(true). - SetContent("you don't have permissions to do this"). - Build()) - if err != nil { - event.Client().Logger().Error("could not respond to reaction: ", err) - return - } - return - } - - messageID := event.Message.ID - message, err := event.Client().Rest().GetMessage(channelWelcomeID, messageID) - if err != nil { - event.Client().Logger().Error("could not find message: ", err) - return - } - if len(message.Mentions) != 1 { - // there should be only one mention in the message - return - } - user := message.Mentions[0] - - // add role to user (bot role needs to be above roleAppleID) - err = event.Client().Rest().AddMemberRole(registerGuildID, user.ID, roleAppleID) - if err != nil { - event.Client().Logger().Error("not able to add role: ", err) - return - } - - // update embed (and time) - if len(message.Embeds) != 1 { - // there should be only one embed in the message - return - } - embed := message.Embeds[0] - updatedMessage := discord.NewEmbedBuilder(). - SetColor(16068661). - SetTimestamp(time.Now()). - SetThumbnail(embed.Thumbnail.URL). - AddFields(embed.Fields...). - AddField("Approval", fmt.Sprintf("%v approved by %v (`%v`)", user.Mention(), approver.Mention(), approver.User.Username), false). - Build() - - err = event.UpdateMessage(discord.NewMessageUpdateBuilder(). - SetContent(message.Content). - SetEmbeds(updatedMessage). - ClearContainerComponents(). - Build()) - if err != nil { - event.Client().Logger().Error("could not update message: ", err) - return - } -}