I tried solving this problem for 2 days already, read like 50 questions in StackOverflow and a lot of documentation about Python. I don't know what else to try.
My Code
import discord
import re
from datetime import datetime
import mysql.connector
from discord.ext import commands, tasks
from discord.utils import get
import atexit
from pynput import keyboard
import asyncio
from asgiref.sync import async_to_sync, sync_to_async
# The currently active modifiers
current = set()
# The key combination to check
COMBINATIONS_MUTE = [
{keyboard.Key.shift, keyboard.KeyCode(char='a')},
{keyboard.Key.shift, keyboard.KeyCode(char='A')}
]
COMBINATIONS_UNMUTE = [
{keyboard.Key.shift, keyboard.KeyCode(char='b')},
{keyboard.Key.shift, keyboard.KeyCode(char='B')}
]
GUILD_ID=guild_id
CHANNELS = ""
GUILD = ""
VOICE_CHANNEL = ""
client = commands.Bot(command_prefix = ".")
def get_channel(name):
global CHANNELS
for channel in CHANNELS:
if (channel.name == name) and (channel.type.name == "voice"):
return channel
return False
def get_voice_channels():
global CHANNELS
channels = []
for channel in CHANNELS:
if (channel.type.name == "voice"):
channels.append(channel)
return channels
async def mute_all():
global VOICE_CHANNELS
print("mute all")
for channel in VOICE_CHANNELS:
for member in channel.members:
await member.edit(mute=True)
async def unmute_all():
global VOICE_CHANNELS
print("unmute all")
for channel in VOICE_CHANNELS:
for member in channel.members:
await member.edit(mute=False)
def on_press(key):
if any([key in COMBO for COMBO in COMBINATIONS_MUTE]) and not key in current:
current.add(key)
if any(all(k in current for k in COMBO) for COMBO in COMBINATIONS_MUTE):
asyncio.run(mute_all())
elif any([key in COMBO for COMBO in COMBINATIONS_UNMUTE]):
current.add(key)
if any(all(k in current for k in COMBO) for COMBO in COMBINATIONS_UNMUTE):
asyncio.run(unmute_all())
def on_release(key):
if any([key in COMBO for COMBO in COMBINATIONS_MUTE]):
current.remove(key)
elif any([key in COMBO for COMBO in COMBINATIONS_UNMUTE]):
current.remove(key)
@client.event
async def on_ready():
global GUILD
global CHANNELS
global VOICE_CHANNELS
global GUILD_ID
GUILD = client.get_guild(GUILD_ID)
CHANNELS = GUILD.channels
VOICE_CHANNELS = get_voice_channels()
print("bot ready")
loop = asyncio.get_event_loop()
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
client.run('token')
Basically what it should do is:
- When I press Shift+A mute everyone in every voice channel.
- When I press Shitf+B unmute everyone in every voice channel.
The problem is that I need to await the member.edit()
but I can't because I can't await mute_all()
nor unmute_all()
because on_press(key)
is not async and I can't make it async because the keyboard_listener won't let me.
What I tried (none worked)
- Using
asyncio.run()
onmember.edit()
. - Using
asyncio.run()
onmute_all()
. - Making
on_press(key)
async. - Using
async_to_sync()
onmember.edit()
. - Using
async_to_sync()
onmute_all()
.
I don't know what else to try.