after.channel
становится None когда Вы выходите из канала.if voice_channel is not None and channel_category is not None:
if after.channel.id == voice_channel:
if voice_channel and channel_category and after.channel.id == voice_channel:
else:
pass
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 22:39:24) [MSC v.1916 32 bit (Intel)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.17.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: def test():
...: for i in range(0, 100):
...: if i%2:
...: 2+2
...: else:
...: pass
...:
In [2]: %timeit test()
14.4 µs ± 559 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [3]: def test():
...: for i in range(0, 100):
...: if i%2:
...: 2+2
...:
In [4]: %timeit test()
13.6 µs ± 435 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
voice_channel_update
в discord.py по умолчанию не существует, и если Вы не вызываете это событие сами, то Вы будете ждать его бесконечно import aiohttp
...
@bot.event
async def on_message(message):
async with aiohttp.ClientSession() as session:
webhook = discord.Webhook.from_url("webhook_url", adapter=discord.AsyncWebhookAdapter(session))
await webhook.send(
message.content,
username=message.author.display_name,
files=[await a.to_file() for a in message.attachments] # https://discordpy.readthedocs.io/en/stable/api.html#discord.Attachment.to_file
)
class MyBot(commands.Bot):
async def on_command_error(self, ctx, e):
if isinstance(e, commands.MissingRequiredArgument):
await ctx.send(f"Недостаточно аргументов: {e.param} отсутствует")
else:
super().on_command_error(ctx, e) # вызвываем оригинальный обработчик ошибок команд
headlinesize = headline.getsize(ctx.author.name)
idraw.text((115 + namesize[0] + 2, 64), f'#{tag}', font=sundertext, fill=(63, 68, 71))
intents
в конструктор объекта бота, например:intents = discord.Intents()
intents.members = True
bot = commands.Bot(..., intents=intents)
724322899921600513
существует, например добавив print(role)
guild.get_role(ID)
вместо discord.utils.get
, оно лучше читается Client.wait_for(event, *, check, timeout)
: https://discordpy.readthedocs.io/en/stable/api.htm...def check(message):
return message.author.id == ctx.author.id
try:
m = await bot.wait_for("message", check=check, timeout = 30)
except asyncio.TimeoutError:
pass
async for ... in history(...)
: https://discordpy.readthedocs.io/en/stable/api.htm...counter = 0
async for message in channel.history(limit=200):
if message.author == client.user:
counter += 1
messages = await channel.history(limit=123).flatten()
# messages is now a list of Message...
from datetime import datetime, timedelta
# ...
async for message in channel.history(
limit=None, # If None, retrieves every message in the channel. Note, however, that this would make it a slow operation.
after=datetime.utcnow()-timedelta(hours=2) # If a date is provided it must be a timezone-naive datetime representing UTC time. (https://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow)
):
command_prefix
можно использовать функцию:The command prefix is what the message content must contain initially to have a command invoked. This prefix could either be a string to indicate what the prefix should be, or a callable that takes in the bot as its first parameter and discord.Message as its second parameter and returns the prefix. This is to facilitate “dynamic” command prefixes. This callable can be either a regular function or a coroutine.
SPECIAL_PREFIX = "."
def context_prefix(bot, message):
special_command = bot.get_command("choose")
if any(
message.content.startswith(f"{SPECIAL_PREFIX}{command_string}")
for command_string in [special_command.name, *special_command.aliases]
):
return SPECIAL_PREFIX
return "&"
bot = commands.Bot(command_prefix=context_prefix)
choose
будет вызываться с префиксом .
, в то время как остальные с префиксом &
: client.event
не должен вызываться: https://discordpy.readthedocs.io/en/v1.4.1/api.htm...@client.event # вызов функции-декоратора (скобки) отсуствует
async def on_ready():
print('Ready!')
on_group_*
на клиенте-боте абсолютно бесполезны, так как дискорд не предоставляет возможности добавления ботов в "группы"on_guild_*
loop.run_in_executor
.import concurrent
...
async def CheckForNewMsgs():
channel = client.get_channel(755448398584479824)
sock = socket.socket() # нужно ли указывать данные аргументы, если они являются стандартными значениями? https://docs.python.org/3/library/socket.html?highlight=socket#socket.socket
server_address = ('127.0.0.1', 6000)
sock.bind(server_address)
sock.listen(10)
while True:
with concurrent.futures.ThreadPoolExecutor() as pool:
connection, client_address = await loop.run_in_executor(pool, sock.accept) # не уверен что из этого блокирует, что нет, в сокеты особо не углублялся
data = connection.recv(16)
serial = data.decode('utf-8')
datas = serial.split('|')
playername = datas[0]
await channel.send(playername + "connected!")
msgs_check_task = asyncio.create_task(CheckForNewMsgs())
# msgs_check_task.cancel() для остановки, подробнее: https://docs.python.org/3/library/asyncio-task.html#asyncio.Task
import concurrent
from discord.ext import tasks
...
@tasks.loop() # будет работать как while True
async def CheckForNewMsgs():
channel = client.get_channel(755448398584479824)
sock = socket.socket() # нужно ли указывать данные аргументы, если они являются стандартными значениями? https://docs.python.org/3/library/socket.html?highlight=socket#socket.socket
server_address = ('127.0.0.1', 6000)
sock.bind(server_address)
sock.listen(10)
with concurrent.futures.ThreadPoolExecutor() as pool:
connection, client_address = await loop.run_in_executor(pool, sock.accept) # не уверен что из этого блокирует, что нет, в сокеты особо не углублялся
data = connection.recv(16)
serial = data.decode('utf-8')
datas = serial.split('|')
playername = datas[0]
await channel.send(playername + "connected!")
CheckForNewMsgs.start()
# CheckForNewMsgs.stop() - остановит после окончания выполнения текущего "прохода", подробнее: https://discordpy.readthedocs.io/en/v1.4.1/ext/tasks/index.html#discord.ext.tasks.Loop.stop
@bot.group()
async def clear(ctx):
"""Строка помощи к [p]clear"""
...
@clear.command(name="all")
async def _all(ctx):
"""Строка помощи для [p]clear all"""
...
@commands.command()
async def command_name(ctx, user: discord.Member):
if role := ctx.guild.get_role(779490160819217032)
await user.add_roles(role)