Skip to content

Instantly share code, notes, and snippets.

@0x001337
Created January 13, 2019 01:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0x001337/c191498287273842dcba517adeb6c185 to your computer and use it in GitHub Desktop.
Save 0x001337/c191498287273842dcba517adeb6c185 to your computer and use it in GitHub Desktop.
The-Eye Member Count Update Script
#! /usr/bin/python3
import discord
import asyncio
import redis
from time import gmtime, strftime
# Discord.py's discord client object
client = discord.Client()
# Redis Database object for storing key:value data, in this case the last person who joined and the last member count
r = redis.Redis(host="localhost", port=6379, db=3, decode_responses=True)
# Server Object
server = None
# The target server ID
server_id = "302796547656253441"
# "Members" voice channel ID
members_channel = "533809556166279168"
# "Newest" voice channel ID
newest_channel = "533809422917697538"
# Function called in order to check the current member count
async def get_members(server):
# Get current server time
time = strftime("%H:%M:%S", gmtime())
# Format the member count, add the thousands seperator decimal (12345 => 12,345)
members = "{:,}".format(server.member_count)
# Update the new member count to the database, in case we want to use it for other applications
r.set("count", server.member_count)
# Print a status update to the console
print(" - Count: " + str(members))
# Edit the "Members" voice channel name with the new count
await client.edit_channel(client.get_channel(members_channel), name="Members: " + str(members))
# Function called whenever the bot is ready
@client.event
async def on_ready():
# Get the server object based on server ID
server = client.get_server(server_id)
# Startup welcome message
print()
print("Logged in as '" + str(client.user.name) + "', server identified as '" + str(server) + "'.")
print()
# Function called whenever someone joins the server
@client.event
async def on_member_join(member):
# Save the new member's username to the Redis database, in case we want to use it for other applications
r.set("newest", member.name)
# Get current server time
time = strftime("%H:%M:%S", gmtime())
# Print a status update to the console
print("[" + str(time) + "] New Member: " + member.name, end='', flush=True)
# Get channel object
ch = client.get_channel(newest_channel)
# Edit the "Newest" voice channel name with the new member's username
await client.edit_channel(ch, name="Newest: " + str(member.name))
# Call the update members function with the server object
await get_members(ch.server)
# Function called whenever someone leaves the server
@client.event
async def on_member_remove(member):
# Get current server time
time = strftime("%H:%M:%S", gmtime())
# Print a status update to the console
print("[" + str(time) + "] Member Left: " + member.name, end='', flush=True)
# Get channel object, in order to get a updated server object
ch = client.get_channel(newest_channel)
# Call the update members function with the server object
await get_members(ch.server)
# Log into the bot account using token
client.run("--snip--")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment