discordBots/cmd/welcomebot/main.go

157 lines
4.1 KiB
Go
Raw Normal View History

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)
2023-01-25 23:33:18 +00:00
log.Info("starting welcomebot...")
log.Info("disgo version: ", disgo.Version)
2023-01-25 23:33:18 +00:00
// permissions: Manage Roles
// intent: Server Members Intent
client, err := disgo.New(token,
bot.WithGatewayConfigOpts(
gateway.WithIntents(gateway.IntentGuildMembers),
),
bot.WithCacheConfigOpts(
2023-01-25 23:33:18 +00:00
cache.WithCaches(
cache.FlagsNone,
),
),
bot.WithEventListenerFunc(joinListener),
bot.WithEventListenerFunc(leaveListener),
)
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")
}
2023-01-25 23:33:18 +00:00
log.Infof("welcomebot 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
2023-01-25 23:33:18 +00:00
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.Member.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
}
}