From 981fc2cb465097a3cd775a2f79597ecc7f179085 Mon Sep 17 00:00:00 2001 From: Seraphim Strub Date: Sun, 22 Jan 2023 15:23:10 +0100 Subject: [PATCH] adds simple join message with reaction to approve --- cmd/joinbot/main.go | 216 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 216 insertions(+) create mode 100644 cmd/joinbot/main.go diff --git a/cmd/joinbot/main.go b/cmd/joinbot/main.go new file mode 100644 index 0000000..54cdbcb --- /dev/null +++ b/cmd/joinbot/main.go @@ -0,0 +1,216 @@ +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 +) + +func main() { + log.SetLevel(log.LevelInfo) + log.Info("starting example...") + log.Info("disgo version: ", disgo.Version) + + // intents needed GUILD_MEMBER_ADD (from GUILD_MEMBERS) + client, err := disgo.New(token, + bot.WithGatewayConfigOpts( + gateway.WithIntents(gateway.IntentGuildMembers), + ), + bot.WithCacheConfigOpts( + cache.WithCacheFlags( + cache.FlagsAll, + ), + ), + bot.WithEventListenerFunc(joinListener), + bot.WithEventListenerFunc(reactionListener), + ) + if err != nil { + log.Fatal("error while building disgo instance: ", err) + return + } + + defer client.Close(context.TODO()) + + if err = client.OpenGateway(context.TODO()); err != nil { + log.Fatal("error while connecting to gateway: ", err) + } + + channels, err := client.Rest().GetGuildChannels(registerGuildID) + for _, channel := range channels { + switch channel.Name() { + case "🔨-rules": + channelRulesMention = channel.Mention() + case "❕-info": + channelInfoMention = channel.Mention() + case "👋-welcome": + channelWelcomeID = channel.ID() + } + } + if channelRulesMention == "" || channelInfoMention == "" || channelWelcomeID == 0 { + log.Fatal("couldn't find needed channel") + } + + roles, err := client.Rest().GetRoles(registerGuildID) + for _, role := range roles { + switch role.Name { + case "Apple": + roleAppleID = role.ID + case "Apple Core": + roleAppleCoreID = role.ID + } + } + if roleAppleID == 0 || roleAppleCoreID == 0 { + log.Fatal("couldn't find needed role") + } + + log.Infof("joinbot is now running. Press CTRL-C to exit.") + s := make(chan os.Signal, 1) + 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 != registerGuildID { + 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 reactionListener(event *events.ComponentInteractionCreate) { + + if event.ChannelID() != channelWelcomeID { + // reaction is not in welcome chat + return + } + + approverID := event.User().ID + if approverID == event.Client().ID() { + // 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 == roleAppleCoreID { + 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 + } +}