A powerful Discord bot built with Go for managing giveaways with MongoDB persistence. Start, manage, and reroll giveaways effortlessly with automatic winner selection and reply-based announcements.
Repository: https://github.com/iimazin11/Go-GiveawayBot
- Create Giveaways - Start giveaways with custom prizes, winner count, and flexible duration
- Button-Based Entry - Users join via emoji button (🎉), no reactions needed
- Automatic End Times - Giveaways auto-end with goroutines at specified times
- Startup Reconciliation - Missed giveaways from downtime are auto-ended on restart
- Winner Selection - Random winner selection with customizable count
- Reroll Support - Select new winners for ended giveaways
- Paginated List - View all giveaways with pagination (5 per page, newest first)
- MongoDB Persistence - All data stored reliably in MongoDB
- Reply Announcements - Winner messages reply directly to giveaway embed
- Permission Checks - MANAGE_GUILD permission required for admin commands
- Go 1.25+
- MongoDB Atlas account (free tier works)
- Discord bot token
- Administrator role in your Discord server
-
Clone the repository
git clone https://github.com/iimazin11/Go-GiveawayBot.git cd "Go-GiveawayBot"
-
Install dependencies
go mod download
-
Create
.envfile in the project root:TOKEN=your_discord_bot_token MONGO=mongodb+srv://username:[email protected]/GiveawayGo?retryWrites=true&w=majority -
Run the bot
go run .
The bot will connect to Discord and MongoDB. You should see: Bot is online: Go App#XXXX
/gstart prize:"iPhone 15" winners:1 duration:"1d"
Parameters:
prize(required) - Prize descriptionwinners(required) - Number of winners to selectduration(required) - Duration format:1s,30m,2h,1d
Duration Examples:
1s= 1 second30m= 30 minutes2h= 2 hours1d= 1 day
After creation, users click the 🎉 button to join. The giveaway automatically ends at the specified time.
/gend giveaway_id:message_id_or_database_id
Parameters:
giveaway_id(required) - Message ID or database ID of the giveaway
Manually ends a giveaway, selects winners randomly, and posts winner announcement as a reply to the giveaway embed.
/greroll giveaway_id:message_id_or_database_id
Parameters:
giveaway_id(required) - Message ID or database ID of the ended giveaway
Selects new random winners and posts reroll announcement with format:
@admin rerolled the giveaway! Congratulations @winner1, @winner2! [↗](message_link)
/glist
Displays all server giveaways paginated (5 per page), sorted by newest first. Use Previous/Next buttons to navigate through pages.
Go Bot/
├── main.go # Bot initialization
├── go.mod # Go module dependencies
├── .env # Configuration (TOKEN, MONGO)
├── README.md # This file
├── db/
│ └── db.go # MongoDB connection
├── models/
│ └── giveaway.go # Giveaway data structure
├── commands/
│ ├── general/
│ │ ├── commands.go # Command registry
│ │ └── ping.go # Ping command
│ └── giveaway/
│ ├── commands.go # Giveaway command registry
│ ├── start.go # /gstart implementation
│ ├── end.go # /gend implementation
│ ├── reroll.go # /greroll implementation
│ ├── list.go # /glist implementation
│ ├── handlers.go # Button handlers & AutoEndGiveaway
│ ├── pagination.go # List pagination handlers
│ └── reconcile.go # Startup reconciliation
└── events/
├── ready.go # Bot ready event
├── interactionCreate.go # Command & button routing
└── messageReactionAdd.go # Reaction handler (no-op)
# Discord Bot Token (get from Discord Developer Portal)
TOKEN=your_bot_token_here
# MongoDB Connection String
MONGO=mongodb+srv://username:[email protected]/GiveawayGo?retryWrites=true&w=majority- Create account at MongoDB Atlas
- Create a free M0 cluster
- Add Network Access - whitelist your IP or use 0.0.0.0/0
- Create database user with password
- Get connection string and replace
<password>with your password - Add to
.envasMONGOvalue
- Go to Discord Developer Portal
- Click "New Application"
- Go to "Bot" section and "Add Bot"
- Copy token and add to
.envasTOKEN - In "OAuth2" → "URL Generator":
- Select scopes:
bot,applications.commands - Select permissions: Send Messages, Read Message History, Manage Messages, Embed Links
- Select scopes:
- Copy generated URL, open in browser, and invite bot to your server
-
Creation - Admin uses
/gstartcommand- Giveaway created in MongoDB with
status: "active" - Embed posted to channel with 🎉 button
- Auto-end goroutine scheduled for end time
- Giveaway created in MongoDB with
-
Active Phase - Users click 🎉 button to join
- User ID added to participants array in database
- Embed updated with current entry count
- User can click again to get leave option
-
End Time - Bot automatically ends giveaway or admin uses
/gend- Random winners selected from participants
- Giveaway status changed to
"ended" - Embed updated with ended color (#2F3136) and disabled button
- Winner announcement posted as reply to embed
-
Reroll - Admin uses
/grerollon ended giveaway- New random winners selected
- Reroll announcement posted as reply with format:
@admin rerolled! Congratulations @winners! [↗](link)
When bot starts, ReconcileEndedGiveaways() automatically:
- Queries database for active giveaways with
endTime ≤ now - Calls
AutoEndGiveaway()for each missed giveaway - Selects winners and posts announcements
- Updates MongoDB with final state
This ensures no giveaways are lost if bot goes offline.
- Color: #5865F2 (Discord Blurple)
- Button: 🎉 emoji (Primary button)
- Shows: Prize, Winners count, Entries count, End time
- Color: #2F3136 (Discord Dark Gray)
- Button: 🎉 Ended (Secondary button, disabled)
- Shows: Winners mentioned, Entries count, End time
All giveaway commands require MANAGE_GUILD permission:
/gstart- Admins only/gend- Admins only/greroll- Admins only/glist- Admins only- Join/Leave Buttons - Anyone
{
_id: ObjectId,
prize: String,
winners: Number,
startTime: Date,
endTime: Date,
participants: [String], // User IDs
selectedWinners: [String], // User IDs
status: String, // "active" or "ended"
createdBy: String, // User ID
guildId: String,
channelId: String,
messageId: String,
giveawayId: String
}Durations use format: <number><unit>
| Format | Meaning |
|---|---|
1s |
1 second |
30m |
30 minutes |
2h |
2 hours |
1d |
1 day |
7d |
7 days |
Examples:
1m= 1 minute15m= 15 minutes12h= 12 hours14d= 14 days
Error: "TOKEN not found in .env"
Solution:
- Verify
.envfile exists in project root directory - Check TOKEN value is correct from Discord Developer Portal
- No spaces before/after the
=sign - Restart bot
Error: "Error connecting to MongoDB"
Solutions:
- Verify
MONGOURI in.envis correct - Check IP whitelist in MongoDB Atlas (Security → Network Access)
- Verify database user password (encode special chars with URL encoding)
- Ensure cluster is running in Atlas dashboard
Problem: Slash commands not visible in Discord
Solutions:
- Verify bot has
applications.commandsscope - Restart bot with
Ctrl+Cthen run again - Type
/in Discord and wait 2 seconds - Check bot invite URL has correct permissions
Problem: 🎉 button doesn't respond
Solutions:
- Ensure bot has "Send Messages" permission
- Check bot is running (check console)
- Message wasn't deleted
- Create new giveaway with
/gstart
Problem: Giveaway passes end time but doesn't automatically end
Solutions:
- If bot was offline, restart bot to trigger reconciliation
- Manually end with
/gend command_id - Check MongoDB connection is active
- Supports 100+ concurrent giveaways
- MongoDB Atlas free tier sufficient for small/medium servers
- One goroutine per active giveaway
- Efficient database indexing on timestamps
go build -o gobot.exe
./gobot.exego build -o gobot
nohup ./gobot > bot.log 2>&1 &FROM golang:1.25
WORKDIR /app
COPY . .
RUN go build -o gobot
CMD ["./gobot"]- Test First - Create a test giveaway with 1 minute duration
- Monitor Logs - Check console output for errors
- Keep Bot Running - 24/7 uptime ensures auto-end works
- Verify Permissions - Admin must have MANAGE_GUILD permission
- Check Whitelist - MongoDB IP whitelist is a common issue
See CONTRIBUTING.md for guidelines on:
- Setting up development environment
- Code style and standards
- Submitting pull requests
- Reporting issues
MIT License - See LICENSE file
- Check logs - Error messages in console indicate the issue
- Verify .env - Most problems stem from incorrect TOKEN or MONGO
- Test connectivity - Ensure bot token and MongoDB URI are valid
- Restart bot - Often solves transient issues
- Check permissions - Verify bot has required Discord permissions
| Command | Usage | Example |
|---|---|---|
| Start | /gstart prize:"" winners:# duration:"" |
/gstart prize:"PS5" winners:2 duration:"1d" |
| End | /gend giveaway_id: |
/gend giveaway_id:1097364528592633856 |
| Reroll | /greroll giveaway_id: |
/greroll giveaway_id:1097364528592633856 |
| List | /glist |
/glist |
Built with ❤️ in Go
Repository: https://github.com/iimazin11/Go-GiveawayBot