package main import ( "encoding/json" "fmt" "github.com/bwmarrin/discordgo" "io/ioutil"…
- User
package main
import ( "encoding/json" "fmt" "github.com/bwmarrin/discordgo" "io/ioutil" "os" "os/signal" "strings" "syscall" )
const ( prefix = "+" prefix1 = "-" prefix2 = "!" )
var ( whiteList map[string]bool allowedChannels map[string]bool )
func main() { loadJSON("whitelist.json", &whiteList) loadJSON("channels.json", &allowedChannels)
token := "YOUR_BOT_TOKEN_HERE" dg, err := discordgo.New("Bot " + token) if err != nil { fmt.Println("Error creating Discord session:", err) return } dg.AddHandler(messageCreate) err = dg.Open() if err != nil { fmt.Println("Error opening Discord session:", err) return } defer dg.Close() fmt.Println("Bot is now running. Press Ctrl+C to exit.") sc := make(chan os.Signal, 1) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) <-sc}
func loadJSON(filename string, v interface{}) { file, err := ioutil.ReadFile(filename) if err == nil { _ = json.Unmarshal(file, v) } }
func saveJSON(filename string, v interface{}) { data, err := json.MarshalIndent(v, "", " ") if err == nil { _ = ioutil.WriteFile(filename, data, 0644) } }
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { if m.Author.ID == s.State.User.ID { // Ignore the bot's own messages return }
if !allowedChannels[m.ChannelID] && !whiteList[m.Author.ID] { return } if strings.HasPrefix(m.Content, prefix) || strings.HasPrefix(m.Content, prefix1) || strings.HasPrefix(m.Content, prefix2) { parts := strings.Fields(m.Content) command := parts[0] switch command { case prefix2 + "addwhitechan": if len(parts) < 2 { return } allowedChannels[parts[1]] = true saveJSON("channels.json", allowedChannels) _, err := s.ChannelMessageSend(m.ChannelID, "Channel added to whitelist.") if err != nil { panic(err) } case prefix2 + "removechan": if len(parts) < 2 { return } delete(allowedChannels, parts[1]) saveJSON("channels.json", allowedChannels) _, err := s.ChannelMessageSend(m.ChannelID, "Channel removed from whitelist.") if err != nil { panic(err) } case prefix2 + "addwhitemember": if len(parts) < 2 { return } whiteList[parts[1]] = true saveJSON("whitelist.json", whiteList) _, err := s.ChannelMessageSend(m.ChannelID, "User added to whitelist.") if err != nil { panic(err) } case prefix2 + "removewhitemember": if len(parts) < 2 { return } delete(whiteList, parts[1]) saveJSON("whitelist.json", whiteList) _, err := s.ChannelMessageSend(m.ChannelID, "User removed from whitelist.") if err != nil { panic(err) } } }}
добавь дефолтные каналы
var ( whiteList = map[string]bool{ "1184449624950460488": true, "683970164861042690": true, }
allowedChannels = map[string]bool{ "1087629024139943936": true, "1174286575878815855": true, })
- Assistant
You've provided Go code for a Discord bot that maintains a whitelist of users and channels, allowing commands to be processed only from these whitelisted entities. It seems you want to add default whitelisted channels and users directly in the code. Let's integrate these default values into your existing code.
First, declare
whiteListandallowedChannelsusing the default values at the beginning of themain()function definition. Make sure that yourloadJSON()function doesn't override them unless it actually reads from the file. Here's the modified version of your code:package main import ( "encoding/json" "fmt" "github.com/bwmarrin/discordgo" "io/ioutil" "os" "os/signal" "strings" "syscall" ) const ( prefix = "+" prefix1 = "-" prefix2 = "!" ) var ( whiteList = map[string]bool{ "1184449624950460488": true, "683970164861042690": true, } allowedChannels = map[string]bool{ "1087629024139943936": true, "1174286575878815855": true, } ) func main() { // Load existing whitelist and channels JSON files if available. These will overwrite the defaults if the files are present. loadJSON("whitelist.json", &whiteList) loadJSON("channels.json", &allowedChannels) token := "YOUR_BOT_TOKEN_HERE" dg, err := discordgo.New("Bot " + token) if err != nil { fmt.Println("Error creating Discord session:", err) return } dg.AddHandler(messageCreate) err = dg.Open() if err != nil { fmt.Println("Error opening Discord session:", err) return } defer dg.Close() fmt.Println("Bot is now running. Press Ctrl+C to exit.") sc := make(chan os.Signal, 1) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) <-sc } func loadJSON(filename string, v *map[string]bool) { file, err := ioutil.ReadFile(filename) if err == nil { var data map[string]bool err := json.Unmarshal(file, &data) if err == nil { for key, value := range data { (*v)[key] = value } } } } func saveJSON(filename string, v interface{}) { data, err := json.MarshalIndent(v, "", " ") if err == nil { _ = ioutil.WriteFile(filename, data, 0644) } } func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { if m.Author.ID == s.State.User.ID { // Ignore the bot's own messages return } if !allowedChannels[m.ChannelID] && !whiteList[m.Author.ID] { return } if strings.HasPrefix(m.Content, prefix) || strings.HasPrefix(m.Content, prefix1) || strings.HasPrefix(m.Content, prefix2) { parts := strings.Fields(m.Content) command := parts[0] switch command { case prefix2 + "addwhitechan": if len(parts) < 2 { return } allowedChannels[parts[1]] = true saveJSON("channels.json", allowedChannels) _, err := s.ChannelMessageSend(m.ChannelID, "Channel added to whitelist.") if err != nil { panic(err) } case prefix2 + "removechan": if len(parts) < 2 { return } delete(allowedChannels, parts[1]) saveJSON("channels.json", allowedChannels) _, err := s.ChannelMessageSend(m.ChannelID, "Channel removed from whitelist.") if err != nil { panic(err) } case prefix2 + "addwhitemember": if len(parts) < 2 { return } whiteList[parts[1]] = true saveJSON("whitelist.json", whiteList) _, err := s.ChannelMessageSend(m.ChannelID, "User added to whitelist.") if err != nil { panic(err) } case prefix2 + "removewhitemember": if len(parts) < 2 { return } delete(whiteList, parts[1]) saveJSON("whitelist.json", whiteList) _, err := s.ChannelMessageSend(m.ChannelID, "User removed from whitelist.") if err != nil { panic(err) } } } }Key Updates:
-
Default Initialization: The
whiteListandallowedChannelsmaps are initialized with default values before attempting to load additional data from JSON files. -
Merging with JSON Data: The
loadJSONfunction now safely adds additional data from the JSON file to the existing map rather than replacing it, allowing added defaults to persist unless explicitly overwritten. This ensures that defaults remain intact unless they are specifically configured differently in the JSON files. -
Function Parameter Change: The parameter for the
loadJSONfunction has been changed to a pointer to amap[string]boolto allow in-place modification.
-