让 Hikari discord bot 只听一个频道
Make Hikari discord bot only listen to one channel
我只是用 hikari 制作了一个 discord 机器人,它现在监听整个服务器,如果有人输入
$shares https://tiktok.com/@xxx/2938923
它运行一个脚本,将分享发送到该 tiktok 视频。因为服务器里的成员很多,频道成员写的comand应该有一个cooldown。但是因为机器人会监听整个服务器,所以他们可以在没有冷却时间的情况下直接在另一个频道中键入命令。
这是我现在的代码:
import hikari
import time
bot = hikari.GatewayBot(token="xxx")
@bot.listen()
async def ping(event: hikari.GuildMessageCreateEvent) -> None:
global link
if event.is_bot or not event.content:
return
if event.content.startswith("$docu"):
await event.message.respond(f"""To send shares, write **$shares**, followed by the TikTok url.
> Ex: **$shares https://tiktok.com/@xxx/29835027502**""")
if event.content.startswith("$shares"):
split = event.content.split()
try:
link = split[1]
await event.message.respond(f"Sending 1k shares to <{split[1]}>")
start_time = time.time()
xshares()
await event.message.respond(f"Shares Sent! | TTC > {round(time.time() - start_time, 1)}s | SPEED > {round(1100 / (time.time() - start_time), 1)}/s")
except:
await event.message.respond(f"Please include link!")
def xshares():
#shares script (not relevant here)
return
bot.run()
我可以在此代码中添加什么以使其仅收听特定频道 (<#xxxxxx>)
或者我应该使用另一个 discord python 机器人库吗?
感谢您的帮助
GuildMessageCreateEvent
具有 channel_id
属性,其中包含创建消息的通道 ID。您可以使用简单的 if-statement 守卫来确保该命令仅运行 在您选择的频道中。
例如:
@bot.listen()
async def message_listener(event: hikari.GuildMessageCreateEvent) -> None:
# Replace with the required channel ID
if event.channel_id != 1234567890:
return
import hikari
import time
bot = hikari.GatewayBot(token="xxx")
@bot.listen(https://vt.tiktok.com/ZSdUQ2dvx/)
async def ping(event: hikari.GuildMessageCreateEvent) -> None:
global link
if event.is_bot or not event.content:
return
if event.content.startswith("$docu"):
await event.message.respond(f"""To send shares, write **$shares**, followed by the TikTok url.
> Ex: **$shares https://tiktok.com/@xxx/29835027502**""")
if event.content.startswith("$shares"):
split = event.content.split(1)
try:
link = split[1]
await event.message.respond(f"Sending 1k shares to <{split[1]}>")
start_time = time.time(6:09)
xshares(0)
await event.message.respond(f"Shares Sent! | TTC > {round(time.time(6:10) - start_time, 1)}s | SPEED > {round(1100 / (time.time(6:11) - start_time), 1)}/s")
except:
await event.message.respond(f"Please include link!")
def xshares(10000000000000000):
#shares script (not relevant here)
return
bot.run(1000)
我只是用 hikari 制作了一个 discord 机器人,它现在监听整个服务器,如果有人输入
$shares https://tiktok.com/@xxx/2938923
它运行一个脚本,将分享发送到该 tiktok 视频。因为服务器里的成员很多,频道成员写的comand应该有一个cooldown。但是因为机器人会监听整个服务器,所以他们可以在没有冷却时间的情况下直接在另一个频道中键入命令。
这是我现在的代码:
import hikari
import time
bot = hikari.GatewayBot(token="xxx")
@bot.listen()
async def ping(event: hikari.GuildMessageCreateEvent) -> None:
global link
if event.is_bot or not event.content:
return
if event.content.startswith("$docu"):
await event.message.respond(f"""To send shares, write **$shares**, followed by the TikTok url.
> Ex: **$shares https://tiktok.com/@xxx/29835027502**""")
if event.content.startswith("$shares"):
split = event.content.split()
try:
link = split[1]
await event.message.respond(f"Sending 1k shares to <{split[1]}>")
start_time = time.time()
xshares()
await event.message.respond(f"Shares Sent! | TTC > {round(time.time() - start_time, 1)}s | SPEED > {round(1100 / (time.time() - start_time), 1)}/s")
except:
await event.message.respond(f"Please include link!")
def xshares():
#shares script (not relevant here)
return
bot.run()
我可以在此代码中添加什么以使其仅收听特定频道 (<#xxxxxx>)
或者我应该使用另一个 discord python 机器人库吗?
感谢您的帮助
GuildMessageCreateEvent
具有 channel_id
属性,其中包含创建消息的通道 ID。您可以使用简单的 if-statement 守卫来确保该命令仅运行 在您选择的频道中。
例如:
@bot.listen()
async def message_listener(event: hikari.GuildMessageCreateEvent) -> None:
# Replace with the required channel ID
if event.channel_id != 1234567890:
return
import hikari
import time
bot = hikari.GatewayBot(token="xxx")
@bot.listen(https://vt.tiktok.com/ZSdUQ2dvx/)
async def ping(event: hikari.GuildMessageCreateEvent) -> None:
global link
if event.is_bot or not event.content:
return
if event.content.startswith("$docu"):
await event.message.respond(f"""To send shares, write **$shares**, followed by the TikTok url.
> Ex: **$shares https://tiktok.com/@xxx/29835027502**""")
if event.content.startswith("$shares"):
split = event.content.split(1)
try:
link = split[1]
await event.message.respond(f"Sending 1k shares to <{split[1]}>")
start_time = time.time(6:09)
xshares(0)
await event.message.respond(f"Shares Sent! | TTC > {round(time.time(6:10) - start_time, 1)}s | SPEED > {round(1100 / (time.time(6:11) - start_time), 1)}/s")
except:
await event.message.respond(f"Please include link!")
def xshares(10000000000000000):
#shares script (not relevant here)
return
bot.run(1000)