Created
November 17, 2022 22:25
-
-
Save etahn-git/6dfb9628f5f8f12b26a89bc4f4127848 to your computer and use it in GitHub Desktop.
Customizable member count channel for discord.py
This file contains 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 | |
import discord.utils | |
from discord.ext import commands | |
# bot token | |
TOKEN = '' | |
# bot prefix | |
BOT_PREFIX = '!' | |
# The id of the channel the bot should update | |
CHANNEL_ID = | |
# Before the member count | |
PREFIX = '🏆 | ' | |
# After the member couunt | |
SUFFIX = 'Members' | |
intents = discord.Intents.default() | |
intents.members = True | |
bot = commands.Bot(command_prefix=BOT_PREFIX, intents=intents) | |
@bot.event | |
async def on_ready(): | |
""" Runs once the bot has established a connection with Discord """ | |
print(f'{bot.user.name} has connected to Discord') | |
# check if bot has connected to guilds | |
if len(bot.guilds) > 0: | |
print('connected to guild:') | |
# list guilds | |
for guild in bot.guilds: | |
# displays guild name and guild id and member count | |
print(f'* {guild.name}#{guild.id}, member count: {len(guild.members)}') | |
# update the member count | |
await update_member_count_channel_name(guild) | |
@bot.event | |
async def on_member_join(member): | |
""" gets triggered when a new member joins a guild """ | |
print(f"* {member} joined {member.guild}") | |
await update_member_count_channel_name(member.guild) | |
@bot.event | |
async def on_member_remove(member): | |
""" gets triggered when a new member leaves or gets removed from a guild """ | |
print(f"* {member} left {member.guild}") | |
await update_member_count_channel_name(member.guild) | |
# Manual Update command | |
@bot.command(name="update") | |
async def on_update_cmd(ctx): | |
""" triggers manual update of member count channel """ | |
print(f"* {ctx.author} issued update") | |
await update_member_count_channel_name(ctx.guild) | |
async def update_member_count_channel_name(guild): | |
""" updates the name of the member count channel """ | |
if CHANNEL_ID != None and SUFFIX != None and PREFIX != None: | |
member_count_channel = discord.utils.get(guild.channels, id=CHANNEL_ID) | |
new_name = f"{PREFIX} {get_guild_member_count(guild)} {SUFFIX}" | |
await member_count_channel.edit(name=new_name) | |
print(f'Channel Displaying: {PREFIX} {get_guild_member_count(guild)} {SUFFIX}') | |
def get_guild_member_count(guild): | |
""" returns the member count of a guild """ | |
return len(guild.members) | |
if __name__ == "__main__": | |
# launch bot | |
bot.run(TOKEN) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment