Last active
July 27, 2021 16:06
-
-
Save AxizY/22df23ce20c089bb4b7f93b4cbcbc433 to your computer and use it in GitHub Desktop.
code for voting bot (very very slopyy)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import discord | |
from discord.ext import commands | |
import sqlite3 | |
conn = sqlite3.connect('new.db') | |
intents = discord.Intents.default() | |
intents.members = True | |
c = conn.cursor() | |
c.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER, vote1 INTEGER, vote2 Integer)") | |
client = commands.Bot(command_prefix = '!',intents=intents) | |
@client.event | |
async def on_ready(): | |
print("The bot is ready") | |
await client.change_presence(activity=discord.Game(name='election or smth')) | |
@client.command() | |
async def firstvote(ctx, member: discord.Member = None): | |
if member is None: | |
await ctx.send("Please choose a person to vote for!") | |
return | |
c.execute(f"SELECT * FROM users WHERE id ={ctx.message.author.id}") | |
res = c.fetchone() | |
if res is not None: | |
if res[1] == member.id or res[2] == member.id: | |
await ctx.send("you already voted for this person") | |
return | |
await ctx.send("vote changed") | |
c.execute(f"UPDATE users SET vote1 = {member.id} WHERE id = {ctx.author.id}") | |
else: | |
await ctx.send("vote set") | |
c.execute(f'INSERT INTO users (id, vote1) VALUES ({ctx.author.id}, {member.id})') | |
conn.commit() | |
@client.command() | |
async def secondvote(ctx, member: discord.Member = None): | |
if member is None: | |
await ctx.send("Please choose a person to vote for!") | |
return | |
c.execute(f"SELECT * FROM users WHERE id ={ctx.message.author.id}") | |
res = c.fetchone() | |
if res is not None: | |
if res[1] == member.id or res[2] == member.id: | |
await ctx.send("you already voted for this person") | |
return | |
await ctx.send("vote changed") | |
c.execute(f"UPDATE users SET vote2 = {member.id} WHERE id = {ctx.author.id}") | |
else: | |
await ctx.send("vote set") | |
c.execute(f'INSERT INTO users (id, vote2) VALUES ({ctx.author.id}, {member.id})') | |
conn.commit() | |
@client.command() | |
async def tally(ctx): | |
c.execute(f"SELECT * FROM users") | |
res = c.fetchall() | |
tall = {} | |
for each in res: | |
if each[1] not in tall: tall.update({each[1]:1}) | |
else: tall.update({each[1]:tall.get(each[1])+1}) | |
if each[2] not in tall: tall.update({each[2]:1}) | |
else: tall.update({each[2]:tall.get(each[2])+1}) | |
build = "" | |
tall = dict(reversed(sorted(tall.items(), key=lambda item: item[1]))) | |
longest = 0 | |
for tal in tall: | |
if len(str(client.get_user(tal))) > longest: | |
longest = len(str(client.get_user(tal))) | |
for tal in tall: | |
build+=("`"+str(client.get_user(tal))+":").ljust(longest+3, " ")+str(tall[tal])+"`\n" | |
await ctx.send(build) | |
client.run('token') | |
conn.commit() | |
c.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment