使用 Python 和 Pycord 在循环中触发方法
Triggering method from within a loop with Python and Pycord
我正在玩 Pycord 并尝试为数组中的每个元素向通道 X 发送消息。
下面的class会抛出一个TypeError: MyClient.on_ready() missing 1 required positional argument: 'message'
表示MyClient.on_ready()
需要2个参数,一个给self
,一个给message
,但是每个例子关于如何将参数传递给 class 方法只是发送参数。
class MyClient(discord.Client):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
async def on_ready(self, message):
channel = self.get_channel(123) # Your channel ID goes here
await channel.send(message)
client = MyClient()
for comment in comments:
if comment['data']['parent_id'].startswith("t1") and comment['data']['name'] not in comments_json:
wop = comment['data']['permalink']
client.on_ready(wop)
client.run("xxx")
我错过了什么?
编辑:添加了评论中的更改,现在错误消息如下:
coroutine 'MyClient.on_ready' was never awaited
client.on_ready(wop)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Ignoring exception in on_ready
Traceback (most recent call last):
File "C:\Python310\lib\site-packages\discord\client.py", line 352, in _run_event
await coro(*args, **kwargs)
TypeError: MyClient.on_ready() missing 1 required positional argument: 'message'
你得到的错误是因为 on_ready
没有接受任何参数
也许您正在寻找这样的东西,获取一个通道对象并发送一个数组(也像您发送的代码一样进行子类化)
import discord
import os
import asyncio
from typing import Iterable, Optional, Any
class MyClient(discord.Client):
# > trying to send a message to a channel X for each element in an array.
def __init__(
self,
channel_to_send: int,
array: Iterable,
*,
loop: Optional[asyncio.AbstractEventLoop] = None,
**options: Any
):
self.channel_to_send = channel_to_send
self.array = array
super().__init__(loop=loop, **options)
async def on_ready(self):
channel_ = await self.fetch_channel(self.channel_to_send)
for element in self.array:
await channel_.send(element)
channel_id = 917803321929134133 # Replace this with that of yours
array = ["foo", "bar", "this", "is", "working"] # Replace this with that of yours
client = MyClient(channel_id, array)
client.run(os.getenv("DISCORD_TOKEN"))
结果:
我正在玩 Pycord 并尝试为数组中的每个元素向通道 X 发送消息。
下面的class会抛出一个TypeError: MyClient.on_ready() missing 1 required positional argument: 'message'
表示MyClient.on_ready()
需要2个参数,一个给self
,一个给message
,但是每个例子关于如何将参数传递给 class 方法只是发送参数。
class MyClient(discord.Client):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
async def on_ready(self, message):
channel = self.get_channel(123) # Your channel ID goes here
await channel.send(message)
client = MyClient()
for comment in comments:
if comment['data']['parent_id'].startswith("t1") and comment['data']['name'] not in comments_json:
wop = comment['data']['permalink']
client.on_ready(wop)
client.run("xxx")
我错过了什么?
编辑:添加了评论中的更改,现在错误消息如下:
coroutine 'MyClient.on_ready' was never awaited
client.on_ready(wop)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Ignoring exception in on_ready
Traceback (most recent call last):
File "C:\Python310\lib\site-packages\discord\client.py", line 352, in _run_event
await coro(*args, **kwargs)
TypeError: MyClient.on_ready() missing 1 required positional argument: 'message'
你得到的错误是因为 on_ready
没有接受任何参数
也许您正在寻找这样的东西,获取一个通道对象并发送一个数组(也像您发送的代码一样进行子类化)
import discord
import os
import asyncio
from typing import Iterable, Optional, Any
class MyClient(discord.Client):
# > trying to send a message to a channel X for each element in an array.
def __init__(
self,
channel_to_send: int,
array: Iterable,
*,
loop: Optional[asyncio.AbstractEventLoop] = None,
**options: Any
):
self.channel_to_send = channel_to_send
self.array = array
super().__init__(loop=loop, **options)
async def on_ready(self):
channel_ = await self.fetch_channel(self.channel_to_send)
for element in self.array:
await channel_.send(element)
channel_id = 917803321929134133 # Replace this with that of yours
array = ["foo", "bar", "this", "is", "working"] # Replace this with that of yours
client = MyClient(channel_id, array)
client.run(os.getenv("DISCORD_TOKEN"))
结果: