This commit is contained in:
100
main.go
100
main.go
@@ -150,6 +150,7 @@ func main() {
|
||||
// Interaction-Handler
|
||||
s := &Server{dg: dg, db: db, tmpl: template.Must(template.New("page").Parse(page))}
|
||||
dg.AddHandler(s.onInteraction)
|
||||
dg.AddHandler(onGuildCreate)
|
||||
|
||||
// Öffnen
|
||||
if err := dg.Open(); err != nil {
|
||||
@@ -196,6 +197,53 @@ func main() {
|
||||
_ = db.Close()
|
||||
}
|
||||
|
||||
func onGuildCreate(s *discordgo.Session, g *discordgo.GuildCreate) {
|
||||
chID := findWritableTextChannel(s, g.Guild)
|
||||
if chID == "" {
|
||||
return
|
||||
}
|
||||
|
||||
embed := &discordgo.MessageEmbed{
|
||||
Title: "👋 Willkommen!",
|
||||
Description: "Ich kann dir Updates per DM schicken.\n\n• Tippe `/subscribe` oder\n• klicke den Button unten, um eine DM mit mir zu starten.",
|
||||
Color: 0x5865F2,
|
||||
}
|
||||
|
||||
components := []discordgo.MessageComponent{
|
||||
discordgo.ActionsRow{
|
||||
Components: []discordgo.MessageComponent{
|
||||
discordgo.Button{
|
||||
CustomID: "start_dm",
|
||||
Label: "DM starten",
|
||||
Style: discordgo.PrimaryButton,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
_, _ = s.ChannelMessageSendComplex(chID, &discordgo.MessageSend{
|
||||
Embed: embed,
|
||||
Components: components,
|
||||
})
|
||||
}
|
||||
|
||||
func findWritableTextChannel(s *discordgo.Session, g *discordgo.Guild) string {
|
||||
chs, err := s.GuildChannels(g.ID)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
for _, c := range chs {
|
||||
if c.Type == discordgo.ChannelTypeGuildText {
|
||||
// Grober Check: hat der Bot Sende-Recht in diesem Channel?
|
||||
perms, err := s.State.UserChannelPermissions(s.State.User.ID, c.ID)
|
||||
if err == nil && (perms&discordgo.PermissionSendMessages) != 0 {
|
||||
return c.ID
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func initDB(db *sql.DB) error {
|
||||
_, err := db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS subscribers (
|
||||
@@ -207,9 +255,50 @@ CREATE TABLE IF NOT EXISTS subscribers (
|
||||
return err
|
||||
}
|
||||
|
||||
func respondEphemeral(dg *discordgo.Session, i *discordgo.InteractionCreate, content string) {
|
||||
_ = dg.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: content,
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func sendDM(dg *discordgo.Session, userID, content string) error {
|
||||
ch, err := dg.UserChannelCreate(userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = dg.ChannelMessageSend(ch.ID, content)
|
||||
return err
|
||||
}
|
||||
|
||||
// onInteraction verarbeitet Slash-Commands (/subscribe, /unsubscribe)
|
||||
// und den User-Kontext-Command („Zu Empfängern hinzufügen“).
|
||||
func (s *Server) onInteraction(_ *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
|
||||
if i.Type == discordgo.InteractionMessageComponent {
|
||||
switch i.MessageComponentData().CustomID {
|
||||
case "start_dm":
|
||||
u := actor(i)
|
||||
if u == nil {
|
||||
respondEphemeral(s.dg, i, "Konnte dich nicht ermitteln.")
|
||||
return
|
||||
}
|
||||
|
||||
// optional: in DB aufnehmen
|
||||
// _ = s.addSub(u.ID, userLabel(u))
|
||||
|
||||
if err := sendDM(s.dg, u.ID, "Hey! Schön, dass du da bist. Ab jetzt kann ich dir DMs schicken. ✨"); err != nil {
|
||||
respondEphemeral(s.dg, i, "DM konnte nicht zugestellt werden (Privacy-Einstellungen?).")
|
||||
return
|
||||
}
|
||||
respondEphemeral(s.dg, i, "Ich habe dir eine DM geschickt. 👍")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Nur Application-Commands behandeln
|
||||
if i.Type != discordgo.InteractionApplicationCommand {
|
||||
return
|
||||
@@ -476,14 +565,3 @@ func (s *Server) listSubs(limit int) ([]subRow, error) {
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ---- DM Helper ----
|
||||
|
||||
func sendDM(s *discordgo.Session, userID, msg string) error {
|
||||
ch, err := s.UserChannelCreate(userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = s.ChannelMessageSend(ch.ID, msg)
|
||||
return err
|
||||
}
|
||||
|
Reference in New Issue
Block a user