2023-01-22 14:23:10 +00:00
|
|
|
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...")
|
2023-01-22 14:23:10 +00:00
|
|
|
log.Info("disgo version: ", disgo.Version)
|
|
|
|
|
2023-01-25 23:33:18 +00:00
|
|
|
// permissions: Manage Roles
|
|
|
|
// intent: Server Members Intent
|
2023-01-22 14:23:10 +00:00
|
|
|
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,
|
2023-01-22 14:23:10 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
bot.WithEventListenerFunc(joinListener),
|
2023-01-28 15:55:44 +00:00
|
|
|
bot.WithEventListenerFunc(leaveListener),
|
2023-01-22 14:23:10 +00:00
|
|
|
)
|
|
|
|
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.")
|
2023-01-22 14:23:10 +00:00
|
|
|
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() {
|
2023-01-22 14:23:10 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-28 15:55:44 +00:00
|
|
|
func leaveListener(event *events.GuildMemberLeave) {
|
2023-01-28 16:04:31 +00:00
|
|
|
user := event.User
|
2023-01-22 14:23:10 +00:00
|
|
|
|
2023-01-28 15:55:44 +00:00
|
|
|
if event.GuildID.String() != registerGuildID.String() {
|
|
|
|
event.Client().Logger().Error("leave from an other guild")
|
2023-01-22 14:23:10 +00:00
|
|
|
return
|
|
|
|
}
|
2023-01-28 15:55:44 +00:00
|
|
|
guild, err := event.Client().Rest().GetGuild(registerGuildID, false)
|
2023-01-22 14:23:10 +00:00
|
|
|
if err != nil {
|
2023-01-28 15:55:44 +00:00
|
|
|
event.Client().Logger().Error("could not find guild: ", err)
|
2023-01-22 14:23:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-28 15:55:44 +00:00
|
|
|
_, err = event.Client().Rest().CreateMessage(channelWelcomeID,
|
|
|
|
discord.NewMessageCreateBuilder().
|
|
|
|
SetContent(
|
|
|
|
fmt.Sprintf("%v (`%v`) left %v", user.Mention(), user.Username, guild.Name)).
|
|
|
|
Build(),
|
|
|
|
)
|
2023-01-22 14:23:10 +00:00
|
|
|
if err != nil {
|
2023-01-28 15:55:44 +00:00
|
|
|
event.Client().Logger().Error("on sending leave message: ", err)
|
2023-01-22 14:23:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|