Discord Poll Bot

By
 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

About this template

This Discord bot allows users to create polls with custom questions, options, and durations. Users can interact with the bot using the "/poll" command to create polls, which are then presented with reaction emojis for voting. The bot tallies votes in real-time and announces the winning option when the poll closes. It is designed to be user-friendly, providing help messages and feedback on command usage.

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!

Category
Technology
Last published
April 7, 2024

More templates like this

Customisable Server Status Dashboard

This template allows you to create a dashboard to report about incidents regarding your server to your users.

Streamlit
Python

A bot that answers questions about data

Ask questions about your database via a chat bot. This chatbot connects to a SQLITE database, generates queries for it based on the schema and then runs the queries printing the response all in a nicely styled chat UI. This template is pre-loaded with sample data (car parts) to play around with - here are the sample data columns that you can query: part_number, part_name, price, units_left_in_stock, manufacturer .

Python
Flask

WhatsApp ChatBot (Twilio, InstantChat)

Build a WhatApp chatbot using Twilio, Discord and LazyAI.

WhatsApp
Twilio
Discord
Home
/
Discord Poll Bot