github.com/meowstercatel/discordgo@v0.0.0-20250615204139-29b43d676735/examples/auto_moderation/main.go (about) 1 package main 2 3 import ( 4 "flag" 5 "fmt" 6 "log" 7 "os" 8 "os/signal" 9 "sync" 10 11 "github.com/bwmarrin/discordgo" 12 ) 13 14 // Command line flags 15 var ( 16 BotToken = flag.String("token", "", "Bot authorization token") 17 GuildID = flag.String("guild", "", "ID of the testing guild") 18 ChannelID = flag.String("channel", "", "ID of the testing channel") 19 ) 20 21 func init() { flag.Parse() } 22 23 func main() { 24 session, _ := discordgo.New("Bot " + *BotToken) 25 session.Identify.Intents |= discordgo.IntentAutoModerationExecution 26 session.Identify.Intents |= discordgo.IntentMessageContent 27 28 enabled := true 29 rule, err := session.AutoModerationRuleCreate(*GuildID, &discordgo.AutoModerationRule{ 30 Name: "Auto Moderation example", 31 EventType: discordgo.AutoModerationEventMessageSend, 32 TriggerType: discordgo.AutoModerationEventTriggerKeyword, 33 TriggerMetadata: &discordgo.AutoModerationTriggerMetadata{ 34 KeywordFilter: []string{"*cat*"}, 35 RegexPatterns: []string{"(c|b)at"}, 36 }, 37 38 Enabled: &enabled, 39 Actions: []discordgo.AutoModerationAction{ 40 {Type: discordgo.AutoModerationRuleActionBlockMessage}, 41 }, 42 }) 43 if err != nil { 44 panic(err) 45 } 46 47 fmt.Println("Successfully created the rule") 48 defer session.AutoModerationRuleDelete(*GuildID, rule.ID) 49 50 session.AddHandlerOnce(func(s *discordgo.Session, e *discordgo.AutoModerationActionExecution) { 51 _, err = session.AutoModerationRuleEdit(*GuildID, rule.ID, &discordgo.AutoModerationRule{ 52 TriggerMetadata: &discordgo.AutoModerationTriggerMetadata{ 53 KeywordFilter: []string{"cat"}, 54 }, 55 Actions: []discordgo.AutoModerationAction{ 56 {Type: discordgo.AutoModerationRuleActionTimeout, Metadata: &discordgo.AutoModerationActionMetadata{Duration: 60}}, 57 {Type: discordgo.AutoModerationRuleActionSendAlertMessage, Metadata: &discordgo.AutoModerationActionMetadata{ 58 ChannelID: e.ChannelID, 59 }}, 60 }, 61 }) 62 if err != nil { 63 session.AutoModerationRuleDelete(*GuildID, rule.ID) 64 panic(err) 65 } 66 67 s.ChannelMessageSend(e.ChannelID, "Congratulations! You have just triggered an auto moderation rule.\n"+ 68 "The current trigger can match anywhere in the word, so even if you write the trigger word as a part of another word, it will still match.\n"+ 69 "The rule has now been changed, now the trigger matches only in the full words.\n"+ 70 "Additionally, when you send a message, an alert will be sent to this channel and you will be **timed out** for a minute.\n") 71 72 var counter int 73 var counterMutex sync.Mutex 74 session.AddHandler(func(s *discordgo.Session, e *discordgo.AutoModerationActionExecution) { 75 action := "unknown" 76 switch e.Action.Type { 77 case discordgo.AutoModerationRuleActionBlockMessage: 78 action = "block message" 79 case discordgo.AutoModerationRuleActionSendAlertMessage: 80 action = "send alert message into <#" + e.Action.Metadata.ChannelID + ">" 81 case discordgo.AutoModerationRuleActionTimeout: 82 action = "timeout" 83 } 84 85 counterMutex.Lock() 86 counter++ 87 if counter == 1 { 88 counterMutex.Unlock() 89 s.ChannelMessageSend(e.ChannelID, "Nothing has changed, right? "+ 90 "Well, since separate gateway events are fired per each action (current is "+action+"), "+ 91 "you'll see a second message about an action pop up soon") 92 } else if counter == 2 { 93 counterMutex.Unlock() 94 s.ChannelMessageSend(e.ChannelID, "Now the second ("+action+") action got executed.") 95 s.ChannelMessageSend(e.ChannelID, "And... you've made it! That's the end of the example.\n"+ 96 "For more information about the automod and how to use it, "+ 97 "you can visit the official Discord docs: https://discord.dev/resources/auto-moderation or ask in our server: https://discord.gg/6dzbuDpSWY", 98 ) 99 100 session.Close() 101 session.AutoModerationRuleDelete(*GuildID, rule.ID) 102 os.Exit(0) 103 } 104 }) 105 }) 106 107 err = session.Open() 108 if err != nil { 109 log.Fatalf("Cannot open the session: %v", err) 110 } 111 defer session.Close() 112 113 stop := make(chan os.Signal, 1) 114 signal.Notify(stop, os.Interrupt) 115 <-stop 116 log.Println("Graceful shutdown") 117 118 }