inc_ipious

US Census Bot Source Code

Jul 23rd, 2021
1,338
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import discord
  2. from discord.ext import commands
  3. import datetime
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6.  
  7. intents = discord.Intents.all()
  8. client = commands.Bot(command_prefix = 'cs!', intents=intents) # You can change the prefix to your taste
  9.  
  10. # makes sure it can only be ran by one person
  11. def user_check(ctx):
  12.     return ctx.author.id ==  123 # Insert user ID of only person who can run commands
  13.  
  14. # activation procedure
  15. @client.event
  16. async def on_ready():
  17.     print('Activated!')
  18.  
  19. # This is just a test command to see if the bot works and responds to the API
  20. @client.command()
  21. async def ping(ctx):
  22.     await ctx.send(f'**Returned at:** {round(client.latency * 1000)}ms')
  23.  
  24. @client.command()
  25. @commands.check(user_check)
  26. async def run(ctx):
  27.     # General Variables
  28.     bot_role = ctx.guild.get_role(788223715669442580) # Need to specify a singular role for ALL BOTS
  29.     bots = len(bot_role.members)
  30.     total_members = int(ctx.guild.member_count - (bots))
  31.     date = datetime.datetime.now()
  32.     todays_date = date.strftime("%A, %B %d, %Y")
  33.  
  34.     # Party Roles ID
  35.     gop = ctx.guild.get_role(123) # FIND THE ROLE ID OF EACH ROLE
  36.     dem = ctx.guild.get_role(123) # FIND THE ROLE ID OF EACH ROLE
  37.     lib = ctx.guild.get_role(123) # FIND THE ROLE ID OF EACH ROLE
  38.     ref = ctx.guild.get_role(123) # FIND THE ROLE ID OF EACH ROLE
  39.     ind = ctx.guild.get_role(123) # FIND THE ROLE ID OF EACH ROLE
  40.     soc = ctx.guild.get_role(123) # FIND THE ROLE ID OF EACH ROLE
  41.     # Add more if you have more
  42.  
  43.     # Number of Members in Role
  44.     total_gop = len(gop.members)
  45.     total_dem = len(dem.members)
  46.     total_lib = len(lib.members)
  47.     total_ref = len(ref.members)
  48.     total_soc = len(soc.members)
  49.     # Add more if you have more
  50.  
  51.     major_parties = (total_gop, total_dem, total_lib, total_ref, total_registered_ind, total_soc)
  52.     sum_mp = int(sum(major_parties))
  53.  
  54.     other_parties = int(total_members - sum_mp)
  55.  
  56.     # Pie Chart
  57.     y = np.array([int(total_gop), int(total_dem), int(total_lib), int(total_ref), int(total_soc), int(total_registered_ind),  int(other_parties)]) # Add more if you have more
  58.     parties = [f"Republicans, {round(float(total_gop/total_members) * 100, 1)}%",
  59.                f"Democrats, {round(float(total_dem/total_members) * 100, 1)}%",
  60.                f"Libertarians, {round(float(total_lib/total_members) * 100, 1)}%",
  61.                f"Reform, {round(float(total_ref/total_members) * 100, 1)}%",
  62.                f"Socialists, {round(float(total_soc / total_members) * 100, 1)}%",
  63.                f"Independents, {round(float(total_registered_ind/total_members) * 100, 1)}%",
  64.                f"Unaffiliated/Other, {round(float(other_parties/total_members) * 100, 1)}%"
  65.                ]
  66.     partisan_colors = ["red", "blue", "yellow", "#00a889", "#7c0e0e", "#6e1cba", "#ededed"] # Make sure partisan colors are in the same pattern as the Pie Chart code block above it
  67.  
  68.     plt.pie(y, labels=parties, colors=partisan_colors)
  69.     filename = date.strftime("%m-%d-%y")
  70.     plt.savefig(f'C:/Location/{filename}.png') # Location where to save the pie charts
  71.  
  72.     pie_chart = discord.File(f'C:/Location/{filename}.png'g', filename=f"{filename}.png") # Find the file location
  73.  
  74.    embed = discord.Embed(
  75.        title="Census Information",
  76.        description=f'**Census Date:** `{todays_date}`'
  77.                    f'\n**Census Year (RP)**: `2010`', # Change the RP year or remove it with the \n entirely
  78.        color=0x4287f5 # Change the color of the embed if you want. Hex code ALWAYS starts with '0x'
  79.    )
  80.    # Raw Numbers
  81.    embed.add_field(name="Total Members", value=str(total_members), inline=True)
  82.    embed.add_field(name="Total Republicans", value=str(total_gop), inline=True)
  83.    embed.add_field(name="Total Democrats", value=str(total_dem), inline=True)
  84.    embed.add_field(name="Total Libertarians", value=str(total_lib), inline=True)
  85.    embed.add_field(name="Total Reform", value=str(total_ref), inline=True)
  86.    embed.add_field(name="Total Socialists", value=str(total_soc), inline=True)
  87.    embed.add_field(name="Total Registered Independents", value=str(total_registered_ind), inline=True)
  88.    embed.add_field(name="Other Parties/Unaffiliated", value=str(other_parties), inline=True)
  89.  
  90.    # Percentages
  91.    embed.set_image(url=f"attachment://{filename}.png")
  92.  
  93.    await ctx.send(file=pie_chart, embed=embed)
  94.  
  95. client.run('BOT TOKEN') # Insert a bot token so the bot actually works
RAW Paste Data