adds leave command and bot to delete all commands for a bot

This commit is contained in:
Seraphim Strub 2023-01-28 16:55:44 +01:00
parent 44c67ffd42
commit fc2a748cd4
2 changed files with 74 additions and 75 deletions

60
cmd/delcombot/main.go Normal file
View file

@ -0,0 +1,60 @@
package main
import (
"context"
"fmt"
"github.com/disgoorg/disgo"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/cache"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/gateway"
"github.com/disgoorg/log"
"os"
)
var (
token = os.Getenv("disgo_token")
noCommands []discord.ApplicationCommandCreate
)
// this bot should in theory delete all registered commands for a certain bot
func main() {
log.SetLevel(log.LevelDebug)
log.Info("starting delcombot...")
log.Info("disgo version: ", disgo.Version)
// permissions:
// intent:
client, err := disgo.New(token,
bot.WithGatewayConfigOpts(
gateway.WithIntents(gateway.IntentsNone),
),
bot.WithCacheConfigOpts(
cache.WithCaches(
cache.FlagsNone,
),
),
)
if err != nil {
log.Fatal("error while building disgo instance: ", err)
return
}
defer client.Close(context.TODO())
client.Caches().GuildsForEach(func(guild discord.Guild) {
if _, err = client.Rest().SetGuildCommands(client.ApplicationID(), guild.ID, noCommands); err != nil {
log.Info(fmt.Sprintf("error deleting guild commands from %v: ", guild.Name), err)
}
})
if _, err = client.Rest().SetGlobalCommands(client.ApplicationID(), noCommands); err != nil {
log.Info("error deleting global commands", err)
}
if err = client.OpenGateway(context.TODO()); err != nil {
log.Fatal("error while connecting to gateway: ", err)
}
log.Infof("delcombot removed all guild and global commands")
}

View file

@ -48,7 +48,7 @@ func main() {
),
),
bot.WithEventListenerFunc(joinListener),
bot.WithEventListenerFunc(reactionListener),
bot.WithEventListenerFunc(leaveListener),
)
if err != nil {
log.Fatal("error while building disgo instance: ", err)
@ -130,88 +130,27 @@ func joinListener(event *events.GuildMemberJoin) {
}
}
func reactionListener(event *events.ComponentInteractionCreate) {
func leaveListener(event *events.GuildMemberLeave) {
user := event.Member.User
if event.ChannelID() != channelWelcomeID {
// reaction is not in welcome chat
if event.GuildID.String() != registerGuildID.String() {
event.Client().Logger().Error("leave from an other guild")
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)
guild, err := event.Client().Rest().GetGuild(registerGuildID, false)
if err != nil {
event.Client().Logger().Error("could not find user: ", err)
event.Client().Logger().Error("could not find guild: ", 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())
_, 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("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)
event.Client().Logger().Error("on sending leave message: ", err)
return
}
}