对于想了解如何让基于Python的DiscordBot随机播放不同的声音?的读者,本文将提供新的信息,我们将详细介绍python自动播放音乐,并且为您提供关于BotDiscordPython:自动发送
对于想了解如何让基于 Python 的 Discord Bot 随机播放不同的声音?的读者,本文将提供新的信息,我们将详细介绍python自动播放音乐,并且为您提供关于Bot Discord Python:自动发送消息的问题、Discord Bot Image Loop (Python)、Discord bot Python API guild.members、Discord Bot Python 提及的有价值信息。
本文目录一览:- 如何让基于 Python 的 Discord Bot 随机播放不同的声音?(python自动播放音乐)
- Bot Discord Python:自动发送消息的问题
- Discord Bot Image Loop (Python)
- Discord bot Python API guild.members
- Discord Bot Python 提及
如何让基于 Python 的 Discord Bot 随机播放不同的声音?(python自动播放音乐)
如何解决如何让基于 Python 的 Discord Bot 随机播放不同的声音??
我正在用 Python 编写一个 discord Bot。 我希望它加入语音频道,然后从列表中随机播放不同的声音。 它可以每隔 10 秒播放一次特定的音频文件。
其他一切正常,机器人连接到一个频道等,但我不知道如何让它留在语音频道上并随机说话。
现在看起来像这样:
arvaus = [ *list of my files here* ]
@client.command()
async def arvaa(ctx):
if not ctx.author.voice:
return await ctx.send(ctx.message.author.mention + ''You need to connect to a voice channel'')
guild = ctx.guild
voice_client: discord.VoiceClient = discord.utils.get(client.voice_clients,guild=guild)
audio_source =(discord.FFmpegPCMAudio(executable="F:/ffmpeg/bin/ffmpeg.exe",source=random.choice(arvaus)))
voice_client.play(audio_source,after=None)
所以基本上现在它会播放一个音频文件,直到我再次输入一个命令来触发另一个。
解决方法
async def play(self,ctx,*,query):
"""Plays a file from the local filesystem"""
source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(query))
ctx.voice_client.play(source,after=lambda e: print(f''Player error: {e}'') if e else None)
await ctx.send(f''Now playing: {query}'')
这是示例之一中的代码 discord_bot_voice myaybe 尝试在您的代码中实现这一点说在机器人连接后播放您拥有的文件中的随机声音
附加:如果你不播放任何声音,机器人会在一段时间后自动断开连接,所以也许只是让机器人播放文件直到随机时间没有声音
也许
arvaus = [ *list of my files here* ]
@client.command()
async def arvaa(ctx):
if not ctx.author.voice:
return await ctx.send(ctx.message.author.mention + ''You need to connect to a voice
channel'')
guild = ctx.guild
voice_client: discord.VoiceClient = discord.utils.get(client.voice_clients,guild=guild)
for i in range(len(arvaus)):
audio_source = (discord.FFmpegPCMAudio(executable="F:/ffmpeg/bin/ffmpeg.exe",source=random.choice(arvaus)))
randomtime = random.randint(20,200)
await asyncio.sleep(ransomint)
voice_client.play(audio_source,after=None)
Bot Discord Python:自动发送消息的问题
您可以使用 client.send
方法直接使用频道,而不是 channel.send
!
async def job():
channel = client.get_channel(ID)
message = "Hello !"
await channel.send(message)
# rest of your code here.
参考:
TextChannel#send
Discord Bot Image Loop (Python)
如何解决Discord Bot Image Loop (Python)?
所以这个 discord Bot 是由三个 Python 脚本组成的。
main.py
GoodAnimeMemes.py
webserver.py
脚本说明:
webserver.py 脚本用于让我的 discord 机器人保持活动状态(24/7 运行)。
GoodAnimeMemes.py 脚本应该从网站上查找图像;在这种情况下,Reddit 并下载它们。
并且 main.py 脚本应该运行 GoodAnimeMemes.py 脚本以循环重新创建新图像,并通过我的 discord 服务器发送它们。
然而,这是我遇到的问题,不知道如何解决。
如何在每次循环结束时重新加载网页,而不使用 webdriver?
我使用的网站(Replit.com)不支持 webdrive 库。
我的代码有效。但分开。如果我运行 GoodAnimeMemes.py,它将根据网站最新的 5 张图片下载新图片。 如果我运行 main.py 它将发送我之前运行的图像。 但是,如果网站发布了新图片,它不会自动下载新图片,而是不断发送相同的图片... 为了这篇文章比较小,我把 main.py 做成只发一张图片。
有什么建议吗?
main.py
import os
import time
import discord
from discord.ext import commands
import GoodAnimeMemes
from webserver import keep_alive
bot = commands.Bot(command_prefix=''$'')
# Server info display in Console when bot runs
@bot.event
async def on_ready():
guild_count = 0
for guild in bot.guilds:
print(f"- {guild.id} (name: {guild.name})")
guild_count = guild_count + 1
print("Bot is in " + str(guild_count) + " guilds.")
# Bot; when command is used,then bot starts
@bot.event
async def on_message(message):
while message.content == "/start":
# Obtain data
exec(open("GoodAnimeMemes.py").read())
# Send data 1
print("! Downloading image...")
await message.channel.send("hey dirtbag",file=discord.File("anime1.jpg"))
print("Sent!")
time.sleep(60)
os.remove("anime1.jpg")
# Pause bot for 4hrs (14400sec)
else:
pass
keep_alive()
TOKEN = os.environ[''disCORD_TOKEN'']
bot.run(TOKEN)
GoodAnimeMemes.py
import requests
from bs4 import BeautifulSoup
import time
headers = {''User-Agent'': xxxxxxxxxxxxxxxxxxxxxxxxxx}
url = "https://www.reddit.com/r/Animemes/"
r = requests.get(url,headers=headers)
# Check connection to website
if r.status_code == 200:
print("Connected to website!")
time.sleep(0.5)
elif r.status_code == 400:
print("Page does not exist...")
time.sleep(0.5)
elif r.status_code == 500:
print("Unauthorized access!")
time.sleep(0.5)
else:
pass
soup = BeautifulSoup(r.text,''html.parser'')
# Print Title of Website
print(soup.title.text)
time.sleep(0.5)
links = soup.find_all("img",{"alt": "Post image"})
# Print How Many Links There Are
print("There are",len(links),"links to work with.")
time.sleep(0.8)
images = []
for link in links:
if link.has_attr(''src''):
# append individual URL to "images" list
images.append(link[''src''])
# Set initial n value
n = 0
# Loop through list of URLs and download each with name "anime" + n
for image in images:
n = n + 1
with open ("anime" + str(n) + ''.jpg'',''wb'') as f:
im = requests.get(image)
f.write(im.content)
版权 (c) 2021,zumbynsr 保留所有权利。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
Discord bot Python API guild.members
如何解决Discord bot Python API guild.members?
我正在尝试制作一个不和谐的机器人,需要从我的公会(服务器)中检索成员。 但是当我运行这段代码时,我得到的 member_count 等于三(服务器中的实际人数。其中一个是机器人)但是当我要求打印它们时,只有一个(机器人)
@client.event
async def on_message(message):
if message.author == client.user:
return
print(message.guild.member_count)
print(len(message.guild.members))
print(message.guild.members)
输出:
>>> 3
>>> 1
>>> <The bots info>
编辑:修正输出
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
Discord Bot Python 提及
如何解决Discord Bot Python 提及?
我需要一个循环,在单独的消息中提及用户 x 次(x 是 0 到 1000 之间的随机选择) 已经试过了:
if message.content.startswith(''$ZSEN''):
await message.channel.send(''MASZYNA LOSUJĄCA JEST PUSTA,I POSZŁO KURWA: '')
for i in range(100):
await message.channel.send(message.author.mention)
解决方法
如果你想得到一个随机数,你必须使用随机库
使用
random.randint(0,1000)
用于导入库 from random import randint
我们今天的关于如何让基于 Python 的 Discord Bot 随机播放不同的声音?和python自动播放音乐的分享已经告一段落,感谢您的关注,如果您想了解更多关于Bot Discord Python:自动发送消息的问题、Discord Bot Image Loop (Python)、Discord bot Python API guild.members、Discord Bot Python 提及的相关信息,请在本站查询。
本文标签: