by Lazy Sloth
Discord Poll Bot
import os
import discord
from discord.ext import commands
from collections import defaultdict
import asyncio
import re
# Retrieve the Discord bot token from the environment variables
DISCORD_BOT_TOKEN = os.environ.get('DISCORD_BOT_TOKEN')
intents = discord.Intents.default()
intents.messages = True
intents.reactions = True
bot = commands.Bot(command_prefix='/', intents=intents)
class Poll:
def __init__(self, question, options, duration=24):
self.question = question + " 🤔"
self.options = options
self.votes = defaultdict(int)
self.message = None
self.duration = duration * 3600 # Convert hours to seconds
self.poll_task = None
Discord Poll Bot
Created: | Last Updated:
Introduction to the Discord Poll Bot Template
Welcome to the Discord Poll Bot template! This template allows you to create a Discord bot that can facilitate polls within your Discord server. Users can interact with the bot using the "/poll" command to create polls with custom questions, options, and durations. The bot will then present the poll with reaction emojis for voting and tally the votes in real-time, announcing the winning option when the poll closes.
Getting Started with the Template
To begin using this template, simply click on "Start with this Template" on the Lazy platform. This will pre-populate the code in the Lazy Builder interface, so you won't need to copy, paste, or delete any code.
Initial Setup: Adding Environment Secrets
Before you can test and use the Discord Poll Bot, you need to set up an environment secret for the Discord bot token. Here's how to acquire and set up your Discord bot token:
- Go to the Discord Developer Portal and create a new application.
- Inside your application, navigate to the "Bot" tab and create a new bot.
- Under the bot settings, you will find the token. Copy this token as you will need it for the next step.
- In the Lazy Builder interface, go to the Environment Secrets tab.
- Create a new secret with the key 'DISCORD_BOT_TOKEN' and paste the token you copied as the value.
Test: Pressing the Test Button
Once you have set up your environment secret, you can press the "Test" button to begin the deployment of your Discord Poll Bot. The Lazy CLI will handle the deployment process, and you won't need to provide any additional input at this stage.
Using the App
After deployment, your Discord Poll Bot will be live on your Discord server. To create a poll, use the "/poll" command followed by the question, duration, and options. For example:
/poll question={Do you like pizza?} duration={1} options={Yes, No, Maybe}
Users can vote by reacting to the poll message with the corresponding emojis. The bot will automatically close the poll after the specified duration and announce the results.
Integrating the App
If you wish to integrate the Discord Poll Bot with other tools or services, you may need to provide the bot's server link or API endpoints. Since this template is designed to work within Discord, no additional external integration steps are required unless you plan to extend the bot's functionality beyond the provided template.
Remember, this bot is designed to work within the Lazy platform, so there's no need to worry about local environments or operating systems. Enjoy creating engaging polls for your Discord community!
Technologies
![Python](https://i.pinimg.com/originals/2f/9c/11/2f9c11f9e55efbf1791f12c06d60729b.jpg)
![Discord](https://media.pocketgamer.biz/2021/5/110514/discord-new-logo-2021-r225x225.jpg)