Discord Bot 开发变得简单!
项目描述
disnake-super-utils
一个现代 python 模块,包括许多有用的功能,使不和谐机器人编程变得非常容易。
文档没有完成。如果您有任何问题,请随时在我们的不和谐服务器中提问。
特征
- 非常易于使用和用户友好。
- 面向对象。
- 现代调平经理。
- 现代音乐/音频播放管理器。[Lavalink 和 FFmpeg 支持]
- 现代异步数据库管理器(SQLite、MongoDB、PostgreSQL、MySQL、MariaDB)。
- 现代分页器。
- 现代反应管理器。
- 现代经济经理。
- 现代图像管理器 (PIL)。
- 现代邀请跟踪器。
- 现代命令提示。
- 现代 FiveM 服务器解析器。
- 现代生日经理。
- 现代前缀管理器。
- 包括易于使用的转换器。
- 为获取玩家而优化的现代 Spotify 客户端。
- 现代惩罚经理(踢,禁令,违规,静音)
- 现代模板管理器。
- 现代 CogManager 支持在不和谐 cogs 中使用管理器。
- 现代消息过滤器和反垃圾邮件。
- 可定制的 ModMail 管理器
- 现代 Youtube 客户端,针对播放器获取进行了优化。
- 还有很多!(更多即将推出!)
安装
安装 disnakeSuperUtils 非常简单。
python -m pip install disnakeSuperUtils
例子
调平示例(使用角色管理器)
import disnake
from disnake.ext import commands
import disnakeSuperUtils
bot = commands.Bot(command_prefix="-", intents=discord.Intents.all())
LevelingManager = disnakeSuperUtils.LevelingManager(bot, award_role=True)
ImageManager = (
disnakeSuperUtils.ImageManager()
) # LevelingManager uses ImageManager to create the rank command.
@bot.event
async def on_ready():
database = disnakeSuperUtils.DatabaseManager.connect(...)
await LevelingManager.connect_to_database(database, ["xp", "roles", "role_list"])
print("Leveling manager is ready.", bot.user)
@LevelingManager.event()
async def on_level_up(message, member_data, roles):
await message.reply(
f"You are now level {await member_data.level()}"
+ (f", you have received the {roles[0]}" f" role." if roles else "")
)
@bot.command()
async def rank(ctx):
member_data = await LevelingManager.get_account(ctx.author)
if not member_data:
await ctx.send(f"I am still creating your account! please wait a few seconds.")
return
guild_leaderboard = await LevelingManager.get_leaderboard(ctx.guild)
member = [x for x in guild_leaderboard if x.member == ctx.author.id]
image = await ImageManager.create_leveling_profile(
ctx.author,
member_data,
disnakeSuperUtils.Backgrounds.GALAXY,
(127, 255, 0),
guild_leaderboard.index(member[0]) + 1 if member else -1,
outline=5,
)
await ctx.send(file=image)
@bot.command()
async def set_roles(ctx, interval: int, *roles: discord.Role):
await LevelingManager.set_interval(ctx.guild, interval)
await LevelingManager.set_roles(ctx.guild, roles)
await ctx.send(
f"Successfully set the interval to {interval} and role list to {', '.join(role.name for role in roles)}"
)
@bot.command()
async def leaderboard(ctx):
guild_leaderboard = await LevelingManager.get_leaderboard(ctx.guild)
formatted_leaderboard = [
f"Member: {x.member}, XP: {await x.xp()}" for x in guild_leaderboard
]
await disnakeSuperUtils.PageManager(
ctx,
disnakeSuperUtils.generate_embeds(
formatted_leaderboard,
title="Leveling Leaderboard",
fields=25,
description=f"Leaderboard of {ctx.guild}",
),
).run()
bot.run("token")
播放示例
从 数学 导入 楼层
从 disnake.ext 导入 命令
从disnakeSuperUtils导入disnakeSuperUtils
导入MusicManager导入disnake
client_id = ""
client_secret = ""
机器人 = 命令。Bot ( command_prefix = "-" , intents = discord . Intents . all ())
# MusicManager = MusicManager(bot, spotify_support=False)
MusicManager = MusicManager (
机器人, client_id = client_id , client_secret = client_secret , spotify_support = True
)
# 如果使用 spotify 支持,请改用这个 ^^^
@音乐经理。event ()
async def on_music_error ( ctx , error ):
raise error # 在此处添加您的错误处理!错误在文档中列出。
@音乐经理。event ()
async def on_queue_end ( ctx ):
print ( f "The queue has ending in { ctx } " )
# 你可以等待并检查活动等...
@音乐经理。event ()
async def on_inactivity_disconnect ( ctx ):
print ( f "I have left { ctx } due to inactivity.." )
@音乐经理。event ()
async def on_play ( ctx , player ):
await ctx 。发送(f “播放{播放器} ” )
@机器人。event
async def on_ready ():
# database = disnakeSuperUtils.DatabaseManager.connect(...)
# await MusicManager.connect_to_database(database, ["playlists"])
print ( "音乐管理器已准备好。" , bot . user )
@机器人。command ()
async def leave ( ctx ):
if await MusicManager 。离开(ctx ):
等待 ctx 。发送(“左声道” )
@机器人。command ()
async def np ( ctx ):
if player := await MusicManager 。now_playing ( ctx ):
duration_played = await MusicManager 。get_player_played_duration ( ctx , player )
# 当然可以格式化。
等待 ctx 。send (
f "当前播放: { player } , \n "
f "时长: { duration_played } / { player . duration } "
)
@机器人。command ()
async def join ( ctx ):
if await MusicManager 。加入(ctx ):
等待 ctx 。发送(“加入语音频道” )
@机器人。组( invoke_without_command = True )
async def 播放列表( ctx , user : discord . User ):
user_playlists = await MusicManager 。get_user_playlists (用户)
formatted_playlists = [
f "ID: ' { user_playlist . id } ' \n Title: ' { user_playlist . playlist . title } ' \n总歌曲数: { len ( user_playlist . playlist . song ) } "
for user_playlist in user_playlists
]
嵌入 = disnakeSuperUtils 。generate_embeds (
formatted_playlists ,
f " { user }的播放列表" ,
f "显示{ user .提及}的播放列表。" ,
25 ,
string_format = " {} " ,
)
page_manager = disnakeSuperUtils 。PageManager ( ctx , embeds , public = True )
等待 page_manager 。运行()
@播放列表。command ()
async def add ( ctx , url : str ):
added_playlist = await MusicManager 。add_playlist ( ctx .作者, url )
如果 没有 添加播放列表:
等待 ctx 。发送(“未找到播放列表 URL!” )
返回
等待 ctx 。发送(f “使用 ID 添加的播放列表{ added_playlist .id } ” )
@播放列表。command ()
async def play ( ctx , playlist_id : str ):
# 这个命令只是一个例子,不是你应该做的。
# 保存的播放列表系统应该提供快速、轻松和简单的播放,用户不应该在播放前寻找
正确的播放列表ID,因为这会破坏整个观点。
# 不要使用播放列表 id 播放,我建议使用索引播放。
# 请,如果您正在使用索引播放,请通过获取所有用户的播放列表来找到您需要的播放列表 id
# 然后从那里找到 id。
# 使用 MusicManager.get_user_playlists(ctx.author, partial=True) 查找用户的播放列表。
# 确保 partial 为 True 以加快获取进度(如果你想访问播放列表数据,
# 你可以将其设置为 False,当然)。
# 使用这些播放列表,找到用户想要的 id,然后播放(或者你想用它做的任何其他事情)。
#有创意!
user_playlist = 等待 音乐管理器。get_playlist ( ctx . author , playlist_id )
如果 不是 user_playlist :
等待 ctx 。send ( "该播放列表不存在!" )
return
如果 不是 ctx 。voice_client 与否 ctx 。_ 语音客户端。is_connected ():等待音乐管理器。加入( ctx )
与 ctx异步。输入():
玩家 = 等待 MusicManager 。create_playlist_players (
user_playlist . playlist , ctx . author
)
如果 玩家:
如果 等待 MusicManager 。queue_add (
players = player , ctx = ctx
) 而 不是 await MusicManager 。播放(ctx ):
等待 ctx 。发送(f “添加播放列表{用户播放列表。播放列表。标题} ” )
否则:
等待 ctx 。发送(“未找到查询。” )
@播放列表。command ()
async def remove ( ctx , playlist_id : str ):
user_playlist = await MusicManager 。get_playlist ( ctx . author , playlist_id )
如果 不是 user_playlist :
等待 ctx 。发送( f "未找到ID为{ playlist_id }的播放列表。" )返回
等待 用户播放列表。删除()
等待 ctx 。send ( f "播放列表{ user_playlist . playlist . title }已被删除" )
@机器人。command ()
async def play ( ctx , * , query : str ):
如果 不是 ctx 。voice_client 与否 ctx 。_ 语音客户端。is_connected ():等待音乐管理器。加入( ctx )
与 ctx异步。输入():
玩家 = 等待 MusicManager 。create_player (查询, ctx .作者)
如果 玩家:
如果 等待 MusicManager 。queue_add (
players = player , ctx = ctx
) 而 不是 await MusicManager 。播放(ctx ):
等待 ctx 。发送(“加入队列” )
否则:
等待 ctx 。发送(“未找到查询。” )
@机器人。command ()
async def Lyrics ( ctx , query : str = None ):
if response := await MusicManager 。歌词( ctx , query ):
title , author , query_lyrics = response
拆分= query_lyrics 。split ( " \n " ) res = [] current = "" for i , split in enumerate ( splitted ): if len ( splitted ) <= i + 1 or len ( current ) + len ( splitted [ i + 1 ]) > 1024 :水库。附加
(当前)
当前 = “”
继续
当前 += 拆分 + “ \n ”
page_manager = disnakeSuperUtils 。PageManager (
ctx ,
[
discord . Embed (
title = f "Lyrics for ' { title } ' by ' { author } ', (Page { i + 1 } / { len ( res ) } )" ,
description = x ,
)
for i , x in enumerate ( res )
],
public = True ,
)
等待 page_manager 。运行()
否则:
等待 ctx 。发送(“未找到歌词。” )
@机器人。command ()
async def pause ( ctx ):
if await MusicManager 。暂停(ctx ):
等待 ctx 。发送(“播放器暂停。” )
@机器人。command ()
async def resume ( ctx ):
if await MusicManager 。恢复(ctx ):
等待 ctx 。发送(“播放器恢复。” )
@机器人。command ()
async def volume ( ctx , volume : int ):
await MusicManager 。体积(ctx , 体积)
@机器人。command ()
async def loop ( ctx ):
is_loop = await MusicManager 。循环( ctx )
如果 is_loop 不是 None :等待ctx 。_ send ( f "循环切换到{ is_loop } " )
@机器人。command ()
async def shuffle ( ctx ):
is_shuffle = await MusicManager 。随机播放( ctx )
如果 is_shuffle 不是 None :等待ctx 。_ 发送( f "随机播放切换到{ is_shuffle } " )
@机器人。command ()
async def autoplay ( ctx ):
is_autoplay = await MusicManager 。自动播放( ctx )
如果 is_autoplay 不是 None :等待ctx 。_ send ( f "自动播放切换到{ is_autoplay } " )
@机器人。command ()
async def queueloop ( ctx ):
is_loop = await MusicManager 。队列循环( ctx )
如果 is_loop 不是 None :等待ctx 。_ send ( f "队列循环切换到{ is_loop } " )
@机器人。command ()
async def complete_queue ( ctx ):
if ctx_queue := await MusicManager 。get_queue ( ctx ):
formatted_queue = [
f "Title: ' { x . title } ' \n请求者: { x . requester and x . requester .提及} \n "
f "职位: { i - ctx_queue 。pos } "
for i , x in enumerate ( ctx_queue.queue ) ] _ _
num_of_fields = 25
嵌入 = disnakeSuperUtils 。generate_embeds (
formatted_queue ,
"Complete Song Queue" ,
"显示完整的歌曲队列。" ,
num_of_fields ,
string_format = " {} " ,
)
page_manager = disnakeSuperUtils 。PageManager (
ctx , embeds , public = True , index = floor ( ctx_queue.pos / 25 ) )等待page_manager 。_ _ 运行()
@机器人。command ()
async def goto ( ctx , position : int ):
if ctx_queue := await MusicManager 。获取队列(ctx ):
new_pos = ctx_queue 。pos + 位置
,如果 不是 0 <= new_pos < len ( ctx_queue.queue ) :等待ctx 。发送(“位置超出范围。” )
返回