Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added file structure example #1540

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions examples/file_structures/Commands/confessions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package commands

import (
"fmt"
"os"

"github.com/bwmarrin/discordgo"
)

func InteractionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
if i.Type == discordgo.InteractionApplicationCommand {
switch i.ApplicationCommandData().Name {
case "confessions":
confessionMessage := ""
for _, opt := range i.ApplicationCommandData().Options {
if opt.Name == "message" && opt.Type == discordgo.ApplicationCommandOptionString {
confessionMessage = opt.StringValue()
break
}
}

channelID := os.Getenv("ChannelID")
embed := &discordgo.MessageEmbed{
Type: discordgo.EmbedTypeRich,
Title: "Confession",
Description: confessionMessage,
Color: 0xff0000,
}

if confessionMessage != "" {
_, err := s.ChannelMessageSendEmbed(channelID, embed)
if err != nil {
fmt.Println("Error sending embed message: ", err)
}
}
}
}
}
17 changes: 17 additions & 0 deletions examples/file_structures/Commands/hello.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package commands

import (
"strings"

"github.com/bwmarrin/discordgo"
)

func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID || m.Author.Bot {
return
}

if strings.HasPrefix(m.Content, "!hello") {
s.ChannelMessageSend(m.ChannelID, "meow meow")
}
}
47 changes: 47 additions & 0 deletions examples/file_structures/Events/load.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package events

import (
"fmt"
"os"
"os/signal"
"syscall"

commands "github.com/Basemint-Community/Confession/Commands"
"github.com/Basemint-Community/Confession/Slash"
"github.com/bwmarrin/discordgo"
)

func ConfessionBot() {
token := os.Getenv("TOKEN")
applicationID := os.Getenv("ApplicationID")

dg, err := discordgo.New("Bot " + token)
if err != nil {
fmt.Println("Error creating Discord session: ", err)
return
}

dg.AddHandler(commands.MessageCreate)
dg.AddHandler(commands.InteractionCreate)

err = dg.Open()
if err != nil {
fmt.Println("Error opening Discord session: ", err)
return
}

err = Slash.RegisterSlashCommand(dg, applicationID)
if err != nil {
fmt.Println("Error registering slash command: ", err)
dg.Close()
return
}

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, os.Kill)
<-sc

dg.Close()
}
43 changes: 43 additions & 0 deletions examples/file_structures/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<img align="right" alt="DiscordGo logo" src="/docs/img/discordgo.svg" width="200">

## DiscordGo

The main moto of this folder is to represent that how you can use the file structure and create clean bots it will also teach you on how to import functions from other folders , it's basic but it's usefull to use a proper file structure

This example demonstrates how to utilize DiscordGo to create a slash commands bot for confessions
this sends confessions directly and secertly to the channel designated

This Bot will respond to "!hello" in any server it's in with "meow meow" in the
sender's DM.

**Join [Discord Gophers](https://discord.gg/0f1SbxBZjYoCtNPP)
Discord chat channel for support.**

### Build

This assumes you already have a working Go environment setup and that
DiscordGo is correctly installed on your system.

From within the file_strcture example folder, run the below command to compile the
example.

```sh
go build
```

### Usage

This example uses bot tokens for authentication only. While user/password is
supported by DiscordGo, it is not recommended for bots.

```
Create a .env File and add ChannelID ApplicationID Token tags and insert the following
Run Command is Simple use ` go run . `
```

The below example shows how to start the bot

```sh
./file_structre go run .
Bot is now running. Press CTRL-C to exit.
```
30 changes: 30 additions & 0 deletions examples/file_structures/Slash/slashcmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package Slash

import (
"fmt"

"github.com/bwmarrin/discordgo"
)

func RegisterSlashCommand(s *discordgo.Session, applicationID string) error {
command := &discordgo.ApplicationCommand{
Name: "confessions",
Description: "Request confessions from the bot with an optional message.",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "message",
Description: "Your confession message",
Required: false,
},
},
}

_, err := s.ApplicationCommandCreate(applicationID, "", command)
if err != nil {
return fmt.Errorf("error registering slash command: %v", err)
}

fmt.Println("Slash command '/confessions' registered successfully.")
return nil
}
14 changes: 14 additions & 0 deletions examples/file_structures/Utils/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package utils

import (
"log"

"github.com/joho/godotenv"
)

func LoadEnv() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
}
20 changes: 20 additions & 0 deletions examples/file_structures/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"fmt"

events "github.com/Basemint-Community/Confession/Events"
utils "github.com/Basemint-Community/Confession/Utils"
)

func init() {
utils.LoadEnv()
events.ConfessionBot()
}

func main() {
fmt.Println("Yes Saar")
}