208 lines
9.8 KiB
Python
208 lines
9.8 KiB
Python
|
|
import json
|
||
|
|
|
||
|
|
import discord
|
||
|
|
from discord.ext import commands
|
||
|
|
|
||
|
|
intents = discord.Intents.all()
|
||
|
|
client = commands.Bot(command_prefix="!", intents=intents)
|
||
|
|
|
||
|
|
# settingsFile = 'settings.json'
|
||
|
|
settingsFile = '/home/ne_ju/pythonProjects/discord/channelBot/settings.json'
|
||
|
|
|
||
|
|
try:
|
||
|
|
settings = json.load(open(settingsFile, 'r'))
|
||
|
|
except FileNotFoundError:
|
||
|
|
settings = {}
|
||
|
|
json.dump(settings, open(settingsFile, 'w'))
|
||
|
|
|
||
|
|
|
||
|
|
@client.event
|
||
|
|
async def on_ready():
|
||
|
|
print('Logged in as {0.user.name}'.format(client))
|
||
|
|
|
||
|
|
|
||
|
|
@client.event
|
||
|
|
async def on_voice_state_update(member, before, after):
|
||
|
|
empty_channels = []
|
||
|
|
category = discord.utils.get(member.guild.categories, name=settings[str(member.guild.id)][0])
|
||
|
|
if after.channel in category.voice_channels or before.channel in category.voice_channels:
|
||
|
|
for channel in category.voice_channels:
|
||
|
|
if len(channel.members) == 0:
|
||
|
|
empty_channels.append(channel)
|
||
|
|
if len(empty_channels) == 0:
|
||
|
|
# print('No empty channels new channel created')
|
||
|
|
await member.guild.create_voice_channel(settings[str(member.guild.id)][1], category=category)
|
||
|
|
elif len(empty_channels) > 1:
|
||
|
|
# print('More than one empty channel deleting channels')
|
||
|
|
while len(empty_channels) > 1:
|
||
|
|
await empty_channels.pop().delete()
|
||
|
|
await empty_channels[0].edit(name=settings[str(member.guild.id)][1])
|
||
|
|
await empty_channels[0].edit(user_limit=0)
|
||
|
|
|
||
|
|
|
||
|
|
@client.event
|
||
|
|
async def on_guild_join(guild):
|
||
|
|
settings[guild.id] = ["Auto Voice Channels", "Voice Channel"]
|
||
|
|
json.dump(settings, open(settingsFile, 'w'))
|
||
|
|
|
||
|
|
|
||
|
|
# @commands.command(name="sync", description="Sync the slash commands")
|
||
|
|
# @commands.is_owner()
|
||
|
|
# async def sync(ctx):
|
||
|
|
# await client.tree.sync()
|
||
|
|
# await ctx.send("Synced commands", delete_after=5)
|
||
|
|
# await ctx.message.delete()
|
||
|
|
|
||
|
|
|
||
|
|
# client.add_command(sync)
|
||
|
|
|
||
|
|
|
||
|
|
# @client.tree.command(name="test", description="Test command")
|
||
|
|
# async def test(interaction: discord.Interaction):
|
||
|
|
# await interaction.response.send_message(content='test', ephemeral=True)
|
||
|
|
|
||
|
|
|
||
|
|
@client.tree.command(name="setup", description="Setup the auto voice channel system")
|
||
|
|
async def setup(interaction: discord.Interaction):
|
||
|
|
if interaction.user.guild_permissions.administrator is False:
|
||
|
|
await interaction.response.send_message(content='You must be an administrator to use this command.',
|
||
|
|
ephemeral=True)
|
||
|
|
return
|
||
|
|
if interaction.guild is None:
|
||
|
|
await interaction.response.send_message(content='This command can only be used in a server.', ephemeral=True)
|
||
|
|
return
|
||
|
|
if str(interaction.guild.id) not in settings:
|
||
|
|
print('No settings found in setup')
|
||
|
|
settings[str(interaction.guild.id)] = ["Auto Voice Channels", "Voice Channel"]
|
||
|
|
json.dump(settings, open(settingsFile, 'w'))
|
||
|
|
category = discord.utils.get(interaction.guild.categories, name=settings[str(interaction.guild.id)][0])
|
||
|
|
if category is None:
|
||
|
|
category = await interaction.guild.create_category(settings[str(interaction.guild.id)][0])
|
||
|
|
if len(category.voice_channels) == 0:
|
||
|
|
await interaction.guild.create_voice_channel(settings[str(interaction.guild.id)][1], category=category)
|
||
|
|
await interaction.response.send_message(content='Setup complete.', ephemeral=True)
|
||
|
|
|
||
|
|
|
||
|
|
@client.tree.command(name="setcategory", description="Set the name of the auto voice channel category")
|
||
|
|
@commands.has_permissions(administrator=True)
|
||
|
|
async def setcategory(interaction: discord.Interaction, category: str):
|
||
|
|
if interaction.user.guild_permissions.administrator is False:
|
||
|
|
await interaction.response.send_message(content='You must be an administrator to use this command.',
|
||
|
|
ephemeral=True)
|
||
|
|
return
|
||
|
|
if interaction.guild is None:
|
||
|
|
await interaction.response.send_message(content='This command can only be used in a server.',
|
||
|
|
ephemeral=True)
|
||
|
|
return
|
||
|
|
if str(interaction.guild.id) not in settings:
|
||
|
|
print('No settings found in setcategory')
|
||
|
|
settings[str(interaction.guild.id)] = ["Auto Voice Channels", "Voice Channel"]
|
||
|
|
|
||
|
|
cat = discord.utils.get(interaction.guild.categories, name=settings[str(interaction.guild.id)][0])
|
||
|
|
settings[str(interaction.guild.id)][0] = category
|
||
|
|
if cat is not None:
|
||
|
|
await cat.edit(name=category)
|
||
|
|
json.dump(settings, open(settingsFile, 'w'))
|
||
|
|
await interaction.response.send_message(content=f'Set category to {category}', ephemeral=True)
|
||
|
|
|
||
|
|
|
||
|
|
@client.tree.command(name="setchannel", description="Set the default name of the voice channels")
|
||
|
|
@commands.has_permissions(administrator=True)
|
||
|
|
async def setchannel(interaction: discord.Interaction, channel: str):
|
||
|
|
if interaction.user.guild_permissions.administrator is False:
|
||
|
|
await interaction.response.send_message(content='You must be an administrator to use this command.',
|
||
|
|
ephemeral=True)
|
||
|
|
return
|
||
|
|
|
||
|
|
if interaction.guild is None:
|
||
|
|
await interaction.response.send_message(content='This command can only be used in a server.',
|
||
|
|
ephemeral=True)
|
||
|
|
return
|
||
|
|
if str(interaction.guild.id) not in settings:
|
||
|
|
print('No settings found in setchannel')
|
||
|
|
settings[str(interaction.guild.id)] = ["Auto Voice Channels", "Voice Channel"]
|
||
|
|
cat = discord.utils.get(interaction.guild.categories, name=settings[str(interaction.guild.id)][0])
|
||
|
|
settings[str(interaction.guild.id)][1] = channel
|
||
|
|
if cat is not None:
|
||
|
|
for vc in cat.voice_channels:
|
||
|
|
if len(vc.members) == 0:
|
||
|
|
await vc.edit(name=channel)
|
||
|
|
json.dump(settings, open(settingsFile, 'w'))
|
||
|
|
await interaction.response.send_message(content=f'Set channel to {channel}', ephemeral=True)
|
||
|
|
|
||
|
|
|
||
|
|
@client.tree.command(name="check_settings", description="Check the settings for the server")
|
||
|
|
@commands.has_permissions(administrator=True)
|
||
|
|
async def check_settings(interaction: discord.Interaction):
|
||
|
|
if interaction.user.guild_permissions.administrator is False:
|
||
|
|
await interaction.response.send_message(content='You must be an administrator to use this command.',
|
||
|
|
ephemeral=True)
|
||
|
|
return
|
||
|
|
|
||
|
|
if interaction.guild is None:
|
||
|
|
await interaction.response.send_message(content='This command can only be used in a server.', ephemeral=True)
|
||
|
|
return
|
||
|
|
if str(interaction.guild.id) not in settings:
|
||
|
|
await interaction.response.send_message(content='No settings found for this server.', ephemeral=True)
|
||
|
|
return
|
||
|
|
await interaction.response.send_message(
|
||
|
|
content=f'Category: {settings[str(interaction.guild.id)][0]}\nChannel: {settings[str(interaction.guild.id)][1]}',
|
||
|
|
ephemeral=True)
|
||
|
|
|
||
|
|
|
||
|
|
@client.tree.command(name="rename", description="Rename your current voice channel")
|
||
|
|
async def rename(interaction: discord.Interaction, name: str):
|
||
|
|
if interaction.guild is None:
|
||
|
|
await interaction.response.send_message(content='This command can only be used in a server.', ephemeral=True)
|
||
|
|
return
|
||
|
|
if interaction.user.voice is None:
|
||
|
|
await interaction.response.send_message(content='You must be in a voice channel to use this command.',
|
||
|
|
ephemeral=True)
|
||
|
|
return
|
||
|
|
if interaction.user.voice.channel is None:
|
||
|
|
await interaction.response.send_message(content='You must be in a voice channel to use this command.',
|
||
|
|
ephemeral=True)
|
||
|
|
return
|
||
|
|
if interaction.user.voice.channel.name == name:
|
||
|
|
await interaction.response.send_message(content='The channel is already named that.', ephemeral=True)
|
||
|
|
return
|
||
|
|
if interaction.user.voice.channel.category.name == settings[str(interaction.user.guild.id)][0]:
|
||
|
|
await interaction.user.voice.channel.edit(name=name)
|
||
|
|
await interaction.response.send_message(content='Channel renamed.', ephemeral=True)
|
||
|
|
return
|
||
|
|
else:
|
||
|
|
await interaction.response.send_message(
|
||
|
|
content=f'You must be in an voice channel under the category {settings[str(interaction.user.guild.id)][0]} to use this command.',
|
||
|
|
ephemeral=True)
|
||
|
|
return
|
||
|
|
|
||
|
|
|
||
|
|
@client.tree.command(name="limit_users", description="Limit the number of users in your current voice channel. 0 for no limit.")
|
||
|
|
async def private(interaction: discord.Interaction, limit: int):
|
||
|
|
if limit < 0 or limit > 99:
|
||
|
|
await interaction.response.send_message(content='Limit must be between 0 and 99.', ephemeral=True)
|
||
|
|
return
|
||
|
|
if interaction.guild is None:
|
||
|
|
await interaction.response.send_message(content='This command can only be used in a server.', ephemeral=True)
|
||
|
|
return
|
||
|
|
if interaction.user.voice is None:
|
||
|
|
await interaction.response.send_message(content='You must be in a voice channel to use this command.',
|
||
|
|
ephemeral=True)
|
||
|
|
return
|
||
|
|
if interaction.user.voice.channel is None:
|
||
|
|
await interaction.response.send_message(content='You must be in a voice channel to use this command.',
|
||
|
|
ephemeral=True)
|
||
|
|
return
|
||
|
|
if interaction.user.voice.channel.category.name == settings[str(interaction.user.guild.id)][0]:
|
||
|
|
await interaction.user.voice.channel.edit(user_limit=limit)
|
||
|
|
await interaction.response.send_message(content='Channel limit set.', ephemeral=True)
|
||
|
|
return
|
||
|
|
else:
|
||
|
|
await interaction.response.send_message(
|
||
|
|
content=f'You must be in an voice channel under the category {settings[str(interaction.user.guild.id)][0]} to use this command.',
|
||
|
|
ephemeral=True)
|
||
|
|
return
|
||
|
|
|
||
|
|
|
||
|
|
client.run('MTE5ODkxMzE3NDQ1NjUxNjY1OQ.GaweoH.sp2xOTG3J_1dMH6PHFqLPySLio7AGcc5jsrpH0')
|