Skip to content

Commit

Permalink
Bug fixes on initial setup process and other cogs
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinTrinh1227 committed Oct 14, 2023
1 parent 88c58cd commit 0df3f5d
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 90 deletions.
126 changes: 70 additions & 56 deletions commands/initial_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,43 +57,9 @@ async def setup(self, ctx):

# Server Stats
await ctx.send("Enable server stats? (0 for No, 1 for Yes):")
try:
server_stats = await self.client.wait_for("message", check=lambda message: message.author == ctx.author, timeout=timeout_time_in_seconds)
config['features']['server_stats'] = int(server_stats.content)

if config['features']['server_stats'] == 1:
# Create the category
category = await guild.create_category('SERVER INFO')

# Create the locked channels under the category
channel_names = ['Member Count: ###', 'Online Members: ###', 'Guild Online: ##/###']
voice_channel_ids = {
'member_count': '',
'members_online': '',
'guild_member_online': ''
}
for channel_name in channel_names:
overwrites = {
guild.default_role: discord.PermissionOverwrite(connect=False)
}
channel = await guild.create_voice_channel(channel_name, overwrites=overwrites, category=category)
print(f'Created channel: {channel.name} ({channel.id})')
for key in voice_channel_ids:
if voice_channel_ids[key] == '':
voice_channel_ids[key] = str(channel.id)

else:
# User chose not to enable server stats, set default IDs to 0
voice_channel_ids = {
'member_count': '0',
'members_online': '0',
'guild_member_online': '0'
}

# update the voice channel data
config['voice_channel_ids'].update(voice_channel_ids)
except asyncio.TimeoutError:
await ctx.send("Timed out. Please try again later.")
server_stats = await self.client.wait_for("message", check=lambda message: message.author == ctx.author, timeout=timeout_time_in_seconds)
config['features']['server_stats'] = int(server_stats.content)
# the server stats category and channels will be generated at the end of the setup process


# Coins & level system question
Expand Down Expand Up @@ -136,22 +102,16 @@ async def setup(self, ctx):

# Staff chat channel
await ctx.send("Reference your bot logs channel. (Mention the channel by using #<channel name>):")
staff_chat_channel_mention = await self.client.wait_for("message", check=lambda message: message.author == ctx.author, timeout = timeout_time_in_seconds)
staff_chat_channel_id = staff_chat_channel_mention.channel_mentions[0].id
config['text_channel_ids']['staff_chat'] = str(staff_chat_channel_id)
bot_logs_channel_mention = await self.client.wait_for("message", check=lambda message: message.author == ctx.author, timeout = timeout_time_in_seconds)
bot_logs_channel_id = bot_logs_channel_mention.channel_mentions[0].id
config['text_channel_ids']['bot_logs'] = str(bot_logs_channel_id)

# Tickets transcripts channel
await ctx.send("Reference your tickets transcripts channel (Mention the channel by using #<channel name>):")
tickets_transcripts_channel_mention = await self.client.wait_for("message", check=lambda message: message.author == ctx.author, timeout = timeout_time_in_seconds)
tickets_transcripts_channel_id = tickets_transcripts_channel_mention.channel_mentions[0].id
config['text_channel_ids']['tickets_transcripts'] = str(tickets_transcripts_channel_id)

# Leave messages channel
await ctx.send("Reference your leave messages channel (Mention the channel by using #<channel name>):")
leave_messages_channel_mention = await self.client.wait_for("message", check=lambda message: message.author == ctx.author, timeout = timeout_time_in_seconds)
leave_messages_channel_id = leave_messages_channel_mention.channel_mentions[0].id
config['text_channel_ids']['leave_messages'] = str(leave_messages_channel_id)

# Daily guild points channel
await ctx.send("Reference your daily guild points channel (Mention the channel by using #<channel name>):")
daily_guild_points_channel_mention = await self.client.wait_for("message", check=lambda message: message.author == ctx.author, timeout = timeout_time_in_seconds)
Expand Down Expand Up @@ -184,15 +144,6 @@ async def setup(self, ctx):
staff_member_role_id = staff_member_role_mention.role_mentions[0].id
config['role_ids']['staff_member'] = str(staff_member_role_id)

# Bots role
await ctx.send("Reference the bots role (Mention the role by using @<role name>):")
bots_role_mention = await self.client.wait_for("message", check=lambda message: message.author == ctx.author, timeout = timeout_time_in_seconds)
bots_role_id = bots_role_mention.role_mentions[0].id
config['role_ids']['bots'] = str(bots_role_id)




# Getting the Hypixel Guild ID
await ctx.send("Enter an IGN of a member that is inside your guild so the bot can retrieve your guild ID:")
guild_member = await self.client.wait_for("message", check=lambda message: message.author == ctx.author, timeout = timeout_time_in_seconds)
Expand Down Expand Up @@ -239,7 +190,70 @@ async def setup(self, ctx):
category = await guild.create_category("TICKETS")
# print(f"Category ID: {category.id}")
config['category_ids']['tickets_category'] = category.id



# creates the server stats channels
try:
if server_stats.content == "1":
# Create the category
category = await guild.create_category('SERVER INFO')

# Get the total member count
total_member_count = len(guild.members)

# Get the number of online members
online_member_count = len(
[member for member in guild.members if member.status != discord.Status.offline])

# Get the guild_member_role_id from data["role_ids"]["guild_member"]
guild_member_role_id_str = data["role_ids"]["guild_member"]

if guild_member_role_id_str:
guild_member_role_id = int(guild_member_role_id_str)
else:
# Handle the case when the string is empty (e.g., set a default value)
guild_member_role_id = 0 # You can change the default value as needed

# Get the number of guild members with the specified role
guild_member_count = len([member for member in guild.members if
guild_member_role_id in [role.id for role in member.roles]])

# Create voice channels with dynamic names
channel_names = [
f'Member Count: {total_member_count}',
f'Online Users: {online_member_count}',
f'Guild Members: {guild_member_count}/125'
]

voice_channel_ids = {
'member_count': '',
'members_online': '',
'guild_member_online': ''
}

for channel_name in channel_names:
overwrites = {
guild.default_role: discord.PermissionOverwrite(connect=False)
}
channel = await guild.create_voice_channel(channel_name, overwrites=overwrites,
category=category)
# print(f'Created channel: {channel.name} ({channel.id}')
for key in voice_channel_ids:
if voice_channel_ids[key] == '':
voice_channel_ids[key] = str(channel.id)
else:
# User chose not to enable server stats, set default IDs to 0
voice_channel_ids = {
'member_count': '0',
'members_online': '0',
'guild_member_online': '0'
}
except Exception as e:
print(f"ERROR: {e}")

# Update the voice channel data
config['voice_channel_ids'].update(voice_channel_ids)



# sets the config bool to 1 aka True so that means that
Expand Down
2 changes: 1 addition & 1 deletion listeners/member_join_leave.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async def on_member_join(self, member):
embed = discord.Embed(
title=(f"Welcome to {member.guild.name}, {member} (#{member_count})"),
description = f"""
Welcome to the {member.guild.name}! Verify your account using `{bot_prefix}link [your IGN]`.
Welcome to the {member.guild.name}! Verify your account using `/verify [your IGN]`.
*THIS IS A PLACEHOLDER WELCOME MESSAGE
YOU CAN EDIT THIS IN "~/Hycord-Bot/listeners/member_join_leave.py"*
Expand Down
64 changes: 31 additions & 33 deletions listeners/server_stats.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import discord
from discord.ext import tasks, commands
import datetime
import json
import pytz
import discord
import requests
import discord.ui
import os
from dotenv import load_dotenv

# Open the JSON file and read in the data
with open('config.json') as json_file:
Expand All @@ -16,7 +10,7 @@
enable_feature = bool(data["features"]["server_stats"])


#json data to get channel IDs
# json data to get channel IDs
command_prefix = data["general"]["bot_prefix"]
member_role_id = int(data["role_ids"]["unverified_member"])
member_count_chanel_id = int(data["voice_channel_ids"]["member_count"])
Expand All @@ -25,27 +19,22 @@
guild_member_role_id = int(data["role_ids"]["guild_member"])


#global variables for channel name usage
# global variables for channel name usage
global_member_count = 0
global_online_members = 0
global_online_and_guild_member = 0


class serverstats(commands.Cog):

def __init__(self, client):
self.client = client
self.serverstats.start()


@tasks.loop(seconds=301.0)
async def serverstats(self):

"""
print(enable_feature)
print(type(enable_feature))
"""

# Open the JSON file and read in the data
with open('config.json') as json_file:
data = json.load(json_file)
Expand All @@ -54,50 +43,59 @@ async def serverstats(self):


#json data to get channel IDs
member_count_chanel_id = int(data["voice_channel_ids"]["member_count"])
members_online_channel_id = int(data["voice_channel_ids"]["members_online"])
guild_member_online_channel_id = int(data["voice_channel_ids"]["guild_member_online"])
member_count_chanel_id = data["voice_channel_ids"]["member_count"]
members_online_channel_id = data["voice_channel_ids"]["members_online"]
guild_member_online_channel_id = data["voice_channel_ids"]["guild_member_online"]
guild_member_role_id = int(data["role_ids"]["guild_member"])

#if the feature is enabled then it will run
if enable_feature:
member_count_channel = self.client.get_channel(member_count_chanel_id) #ID of voice channel that changes
members_online_channel = self.client.get_channel(members_online_channel_id) #ID of voice channel online members
guild_member_online_channel = self.client.get_channel(guild_member_online_channel_id) #guild_member online voice channel

member_count_channel = self.client.get_channel(int(member_count_chanel_id)) #ID of voice channel that changes
members_online_channel = self.client.get_channel(int(members_online_channel_id)) #ID of voice channel online members
guild_member_online_channel = self.client.get_channel(int(guild_member_online_channel_id)) #guild_member online voice channel
guild_member_role = discord.utils.get(self.client.guilds[0].roles, id=guild_member_role_id)


#if a change has been detected.
#this helps with being rate limited by discord

member_count = len(self.client.guilds[0].members)
global global_member_count
if (global_member_count != member_count):
global_member_count = member_count
await member_count_channel.edit(name=f"Member Count: {member_count}")
try:
await member_count_channel.edit(name=f"Member Count: {member_count}")
except:
pass
else:
pass

online_members = [member for member in self.client.guilds[0].members if member.status != discord.Status.offline]
global global_online_members
if (global_online_members != online_members):
global_online_members = online_members
await members_online_channel.edit(name=f"Online Members: {len(online_members)}")
try:
await members_online_channel.edit(name=f"Online Members: {len(online_members)}")
except:
pass
else:
pass

online_and_guild_member_members = [member for member in self.client.guilds[0].members if guild_member_role in member.roles and member.status != discord.Status.offline]
global global_online_and_guild_member
if (global_online_and_guild_member != online_and_guild_member_members):
global_online_and_guild_member = online_and_guild_member_members
await guild_member_online_channel.edit(name=f"Guild Online: {len(online_and_guild_member_members)}/125")
try:
await guild_member_online_channel.edit(name=f"Guild Online: {len(online_and_guild_member_members)}/125")
except:
pass
else:
pass

else:
pass


async def setup(client):
await client.add_cog(serverstats(client))

0 comments on commit 0df3f5d

Please sign in to comment.