我有一个不和谐的机器人。
我想要它做的是,当机器人检测到用户正在直播时,将其直播的 URL 打印到聊天中。
我在尝试找出如何获取用户的 twitch 频道 url 时遇到问题...
这就是我所拥有的
@client.event
async def on_member_update(before, after):
aname = after.display_name
aactivity = after.activity.type
mactivity = str(after.activities)
role = after.guild.get_role(736271867836498031)
channel = client.get_channel(736269005651836985)
memberinfo = after.activities.streaming.url
print(memberinfo)
if "Streaming name" in mactivity:
await after.add_roles(role)
await channel.send('{} has started streaming!'.format(after.display_name))
print(aname + " is streaming!")
else:
await after.remove_roles(role)
streamchannel = discord.Profile
await channel.send('{} is not streaming!'.format(after.display_name))
print(aname + " is not streaming.")
print('member updated status')
我有什么遗漏的吗?我根本不知道如何找到主播的 URL,或者从他们的连接帐户中获取它。
Member
对象具有 Activity
属性。您可以检查 before.activity
或 after.activity
是否是 Streaming
类的实例。channel
: 中发送消息
from discord import Streaming
from discord.utils import get
@client.event
async def on_member_update(before, after):
if not before.activity.type == after.activity.type:
return
role = get(after.guild.roles, id=736271867836498031)
channel = get(after.guild.channels, id=736269005651836985)
if isinstance(after.activity, Streaming):
await after.add_roles(role)
await channel.send(f"{before.mention} is streaming on {activity.platform}: {activity.name}.\nJoin here: {activity.url}")
elif isinstance(before.activity, Streaming):
await after.remove_roles(role)
await channel.send(f'{after.mention} is no longer streaming!')
else:
return
参考: discord.py 文档
他告诉我没有错误,但他没有发出任何通知
import os
import platform
import time
import discord
from colorama import Back, Fore, Style
from discord import Streaming
from discord.ext import commands
from discord.utils import get
client = commands.Bot(command_prefix='!', intents=discord.Intents.all())
@client.command()
async def hello(ctx):
await ctx.send("Hello!")
@client.event
async def on_ready():
prfx = (Back.BLACK + Fore.GREEN + time.strftime("%H:%M:%S UTC", time.gmtime()) + Back.RESET + Fore.WHITE + Style.BRIGHT)
print(prfx + " Logged in as " + Fore.YELLOW + client.user.name)
print(prfx + " Bot ID " + Fore.YELLOW + str(client.user.id))
print(prfx + " Discord Version " + Fore.YELLOW + discord.__version__)
print(prfx + " Python Version " + Fore.YELLOW + str(platform.python_version()))
@client.command(aliases=['close, stop'])
async def shutdown(ctx):
await ctx.send("Shutting down the bot!")
await client.close()
#### Nochmal anschauen
@client.command(aliases=['user, info'])
async def userinfo(ctx, member:discord.Member):
embed = discord.Embed(title="User Info", description=f"Here's the user info of the user {member.name}", color = discord.Color.green(), timestamp = ctx.message.created_at)
await ctx.send(embed=embed)
#####
@client.event
async def on_member_update(self, before, after):
if after.guild.id == 1196653515494400071:
if before.activity == after.activity:
return
role = get(after.guild.roles, id=1196675993025380453)
channel = get(after.guild.channels, id=1196678161799331960)
async for message in channel.history(limit=10):
if before.mention in message.content and "is now streaming" in message.content:
if isinstance(after.activity, Streaming):
return
if isinstance(after.activity, Streaming):
await after.add_roles(role)
stream_url = after.activity.url
stream_url_split = stream_url.split(".")
streaming_service = stream_url_split[1]
streaming_service = streaming_service.capitalize()
await channel.send(f":red_circle: **LIVE**\n{before.mention} is now streaming on {Twitch}!\n{stream_url}")
elif isinstance(before.activity, Streaming):
await after.remove_roles(role)
async for message in channel.history(limit=10):
if before.mention in message.content and "is now streaming" in message.content:
await message.delete()
else:
return
print("Server Running")
client.run(os.getenv("TOKEN"))