mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-10 17:28:27 -04:00
Added .autopublish command
This commit is contained in:
@@ -2,6 +2,12 @@
|
|||||||
|
|
||||||
Experimental changelog. Mostly based on [keepachangelog](https://keepachangelog.com/en/1.0.0/) except date format. a-c-f-r-o
|
Experimental changelog. Mostly based on [keepachangelog](https://keepachangelog.com/en/1.0.0/) except date format. a-c-f-r-o
|
||||||
|
|
||||||
|
## Unreleased
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- Added `.autopublish` command which will automatically publish messages posted in the channel.
|
||||||
|
|
||||||
## [4.3.7]
|
## [4.3.7]
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
9
src/NadekoBot/Db/Models/AutoPublishChannel.cs
Normal file
9
src/NadekoBot/Db/Models/AutoPublishChannel.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using NadekoBot.Services.Database.Models;
|
||||||
|
|
||||||
|
namespace NadekoBot.Db.Models;
|
||||||
|
|
||||||
|
public class AutoPublishChannel : DbEntity
|
||||||
|
{
|
||||||
|
public ulong GuildId { get; set; }
|
||||||
|
public ulong ChannelId { get; set; }
|
||||||
|
}
|
@@ -464,6 +464,14 @@ public abstract class NadekoContext : DbContext
|
|||||||
});
|
});
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region AutoPublish
|
||||||
|
|
||||||
|
modelBuilder.Entity<AutoPublishChannel>(apc => apc
|
||||||
|
.HasIndex(x => x.GuildId)
|
||||||
|
.IsUnique());
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
|
3579
src/NadekoBot/Migrations/Mysql/20220916194514_autopub.Designer.cs
generated
Normal file
3579
src/NadekoBot/Migrations/Mysql/20220916194514_autopub.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
42
src/NadekoBot/Migrations/Mysql/20220916194514_autopub.cs
Normal file
42
src/NadekoBot/Migrations/Mysql/20220916194514_autopub.cs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
using System;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace NadekoBot.Migrations.Mysql
|
||||||
|
{
|
||||||
|
public partial class autopub : Migration
|
||||||
|
{
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "autopublishchannel",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
id = table.Column<int>(type: "int", nullable: false)
|
||||||
|
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
|
||||||
|
guildid = table.Column<ulong>(type: "bigint unsigned", nullable: false),
|
||||||
|
channelid = table.Column<ulong>(type: "bigint unsigned", nullable: false),
|
||||||
|
dateadded = table.Column<DateTime>(type: "datetime(6)", nullable: true)
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("pk_autopublishchannel", x => x.id);
|
||||||
|
})
|
||||||
|
.Annotation("MySql:CharSet", "utf8mb4");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "ix_autopublishchannel_guildid",
|
||||||
|
table: "autopublishchannel",
|
||||||
|
column: "guildid",
|
||||||
|
unique: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "autopublishchannel");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -19,6 +19,35 @@ namespace NadekoBot.Migrations.Mysql
|
|||||||
.HasAnnotation("ProductVersion", "6.0.7")
|
.HasAnnotation("ProductVersion", "6.0.7")
|
||||||
.HasAnnotation("Relational:MaxIdentifierLength", 64);
|
.HasAnnotation("Relational:MaxIdentifierLength", 64);
|
||||||
|
|
||||||
|
modelBuilder.Entity("NadekoBot.Db.Models.AutoPublishChannel", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int")
|
||||||
|
.HasColumnName("id");
|
||||||
|
|
||||||
|
b.Property<ulong>("ChannelId")
|
||||||
|
.HasColumnType("bigint unsigned")
|
||||||
|
.HasColumnName("channelid");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DateAdded")
|
||||||
|
.HasColumnType("datetime(6)")
|
||||||
|
.HasColumnName("dateadded");
|
||||||
|
|
||||||
|
b.Property<ulong>("GuildId")
|
||||||
|
.HasColumnType("bigint unsigned")
|
||||||
|
.HasColumnName("guildid");
|
||||||
|
|
||||||
|
b.HasKey("Id")
|
||||||
|
.HasName("pk_autopublishchannel");
|
||||||
|
|
||||||
|
b.HasIndex("GuildId")
|
||||||
|
.IsUnique()
|
||||||
|
.HasDatabaseName("ix_autopublishchannel_guildid");
|
||||||
|
|
||||||
|
b.ToTable("autopublishchannel", (string)null);
|
||||||
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("NadekoBot.Db.Models.BankUser", b =>
|
modelBuilder.Entity("NadekoBot.Db.Models.BankUser", b =>
|
||||||
{
|
{
|
||||||
b.Property<int>("Id")
|
b.Property<int>("Id")
|
||||||
|
3725
src/NadekoBot/Migrations/PostgreSql/20220916194523_autopub.Designer.cs
generated
Normal file
3725
src/NadekoBot/Migrations/PostgreSql/20220916194523_autopub.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,51 @@
|
|||||||
|
using System;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace NadekoBot.Migrations.PostgreSql
|
||||||
|
{
|
||||||
|
public partial class autopub : Migration
|
||||||
|
{
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.AddColumn<decimal>(
|
||||||
|
name: "rolerequirement",
|
||||||
|
table: "shopentry",
|
||||||
|
type: "numeric(20,0)",
|
||||||
|
nullable: true);
|
||||||
|
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "autopublishchannel",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
id = table.Column<int>(type: "integer", nullable: false)
|
||||||
|
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
|
||||||
|
guildid = table.Column<decimal>(type: "numeric(20,0)", nullable: false),
|
||||||
|
channelid = table.Column<decimal>(type: "numeric(20,0)", nullable: false),
|
||||||
|
dateadded = table.Column<DateTime>(type: "timestamp without time zone", nullable: true)
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("pk_autopublishchannel", x => x.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "ix_autopublishchannel_guildid",
|
||||||
|
table: "autopublishchannel",
|
||||||
|
column: "guildid",
|
||||||
|
unique: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "autopublishchannel");
|
||||||
|
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "rolerequirement",
|
||||||
|
table: "shopentry");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -22,6 +22,37 @@ namespace NadekoBot.Migrations.PostgreSql
|
|||||||
|
|
||||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||||
|
|
||||||
|
modelBuilder.Entity("NadekoBot.Db.Models.AutoPublishChannel", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("integer")
|
||||||
|
.HasColumnName("id");
|
||||||
|
|
||||||
|
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<decimal>("ChannelId")
|
||||||
|
.HasColumnType("numeric(20,0)")
|
||||||
|
.HasColumnName("channelid");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DateAdded")
|
||||||
|
.HasColumnType("timestamp without time zone")
|
||||||
|
.HasColumnName("dateadded");
|
||||||
|
|
||||||
|
b.Property<decimal>("GuildId")
|
||||||
|
.HasColumnType("numeric(20,0)")
|
||||||
|
.HasColumnName("guildid");
|
||||||
|
|
||||||
|
b.HasKey("Id")
|
||||||
|
.HasName("pk_autopublishchannel");
|
||||||
|
|
||||||
|
b.HasIndex("GuildId")
|
||||||
|
.IsUnique()
|
||||||
|
.HasDatabaseName("ix_autopublishchannel_guildid");
|
||||||
|
|
||||||
|
b.ToTable("autopublishchannel", (string)null);
|
||||||
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("NadekoBot.Db.Models.BankUser", b =>
|
modelBuilder.Entity("NadekoBot.Db.Models.BankUser", b =>
|
||||||
{
|
{
|
||||||
b.Property<int>("Id")
|
b.Property<int>("Id")
|
||||||
|
2872
src/NadekoBot/Migrations/Sqlite/20220916191702_autopub.Designer.cs
generated
Normal file
2872
src/NadekoBot/Migrations/Sqlite/20220916191702_autopub.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
40
src/NadekoBot/Migrations/Sqlite/20220916191702_autopub.cs
Normal file
40
src/NadekoBot/Migrations/Sqlite/20220916191702_autopub.cs
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
using System;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace NadekoBot.Migrations
|
||||||
|
{
|
||||||
|
public partial class autopub : Migration
|
||||||
|
{
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "AutoPublishChannel",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<int>(type: "INTEGER", nullable: false)
|
||||||
|
.Annotation("Sqlite:Autoincrement", true),
|
||||||
|
GuildId = table.Column<ulong>(type: "INTEGER", nullable: false),
|
||||||
|
ChannelId = table.Column<ulong>(type: "INTEGER", nullable: false),
|
||||||
|
DateAdded = table.Column<DateTime>(type: "TEXT", nullable: true)
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_AutoPublishChannel", x => x.Id);
|
||||||
|
});
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_AutoPublishChannel_GuildId",
|
||||||
|
table: "AutoPublishChannel",
|
||||||
|
column: "GuildId",
|
||||||
|
unique: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "AutoPublishChannel");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -17,6 +17,29 @@ namespace NadekoBot.Migrations
|
|||||||
#pragma warning disable 612, 618
|
#pragma warning disable 612, 618
|
||||||
modelBuilder.HasAnnotation("ProductVersion", "6.0.7");
|
modelBuilder.HasAnnotation("ProductVersion", "6.0.7");
|
||||||
|
|
||||||
|
modelBuilder.Entity("NadekoBot.Db.Models.AutoPublishChannel", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.Property<ulong>("ChannelId")
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DateAdded")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<ulong>("GuildId")
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("GuildId")
|
||||||
|
.IsUnique();
|
||||||
|
|
||||||
|
b.ToTable("AutoPublishChannel");
|
||||||
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("NadekoBot.Db.Models.BankUser", b =>
|
modelBuilder.Entity("NadekoBot.Db.Models.BankUser", b =>
|
||||||
{
|
{
|
||||||
b.Property<int>("Id")
|
b.Property<int>("Id")
|
||||||
|
@@ -33,9 +33,13 @@ public partial class Administration : NadekoModule<AdministrationService>
|
|||||||
}
|
}
|
||||||
|
|
||||||
private readonly SomethingOnlyChannelService _somethingOnly;
|
private readonly SomethingOnlyChannelService _somethingOnly;
|
||||||
|
private readonly AutoPublishService _autoPubService;
|
||||||
|
|
||||||
public Administration(SomethingOnlyChannelService somethingOnly)
|
public Administration(SomethingOnlyChannelService somethingOnly, AutoPublishService autoPubService)
|
||||||
=> _somethingOnly = somethingOnly;
|
{
|
||||||
|
_somethingOnly = somethingOnly;
|
||||||
|
_autoPubService = autoPubService;
|
||||||
|
}
|
||||||
|
|
||||||
[Cmd]
|
[Cmd]
|
||||||
[RequireContext(ContextType.Guild)]
|
[RequireContext(ContextType.Guild)]
|
||||||
@@ -376,4 +380,26 @@ public partial class Administration : NadekoModule<AdministrationService>
|
|||||||
await t.DeleteAsync();
|
await t.DeleteAsync();
|
||||||
await ctx.OkAsync();
|
await ctx.OkAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Cmd]
|
||||||
|
[UserPerm(ChannelPerm.ManageMessages)]
|
||||||
|
public async Task AutoPublish()
|
||||||
|
{
|
||||||
|
if (ctx.Channel.GetChannelType() != ChannelType.News)
|
||||||
|
{
|
||||||
|
await ReplyErrorLocalizedAsync(strs.req_announcement_channel);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var newState = await _autoPubService.ToggleAutoPublish(ctx.Guild.Id, ctx.Channel.Id);
|
||||||
|
|
||||||
|
if (newState)
|
||||||
|
{
|
||||||
|
await ReplyConfirmLocalizedAsync(strs.autopublish_enable);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await ReplyConfirmLocalizedAsync(strs.autopublish_disable);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@@ -1,6 +1,5 @@
|
|||||||
#nullable disable
|
#nullable disable
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Nadeko.Common;
|
|
||||||
using NadekoBot.Db;
|
using NadekoBot.Db;
|
||||||
using NadekoBot.Services.Database.Models;
|
using NadekoBot.Services.Database.Models;
|
||||||
|
|
||||||
|
87
src/NadekoBot/Modules/Administration/AutoPublishService.cs
Normal file
87
src/NadekoBot/Modules/Administration/AutoPublishService.cs
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
#nullable disable
|
||||||
|
using LinqToDB;
|
||||||
|
using LinqToDB.EntityFrameworkCore;
|
||||||
|
using NadekoBot.Common.ModuleBehaviors;
|
||||||
|
using NadekoBot.Db.Models;
|
||||||
|
|
||||||
|
namespace NadekoBot.Modules.Administration.Services;
|
||||||
|
|
||||||
|
public class AutoPublishService : IExecNoCommand, IReadyExecutor, INService
|
||||||
|
{
|
||||||
|
private readonly DbService _db;
|
||||||
|
private readonly DiscordSocketClient _client;
|
||||||
|
private readonly IBotCredsProvider _creds;
|
||||||
|
private ConcurrentDictionary<ulong, ulong> _enabled;
|
||||||
|
|
||||||
|
public AutoPublishService(DbService db, DiscordSocketClient client, IBotCredsProvider creds)
|
||||||
|
{
|
||||||
|
_db = db;
|
||||||
|
_client = client;
|
||||||
|
_creds = creds;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task ExecOnNoCommandAsync(IGuild guild, IUserMessage msg)
|
||||||
|
{
|
||||||
|
if (guild is null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (msg.Channel.GetChannelType() != ChannelType.News)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!_enabled.TryGetValue(guild.Id, out var cid) || cid != msg.Channel.Id)
|
||||||
|
return;
|
||||||
|
|
||||||
|
await msg.CrosspostAsync(new RequestOptions()
|
||||||
|
{
|
||||||
|
RetryMode = RetryMode.AlwaysFail
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task OnReadyAsync()
|
||||||
|
{
|
||||||
|
var creds = _creds.GetCreds();
|
||||||
|
|
||||||
|
await using var ctx = _db.GetDbContext();
|
||||||
|
var items = await ctx.GetTable<AutoPublishChannel>()
|
||||||
|
.Where(x => Linq2DbExpressions.GuildOnShard(x.GuildId, creds.TotalShards, _client.ShardId))
|
||||||
|
.ToListAsyncLinqToDB();
|
||||||
|
|
||||||
|
_enabled = items
|
||||||
|
.ToDictionary(x => x.GuildId, x => x.ChannelId)
|
||||||
|
.ToConcurrent();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> ToggleAutoPublish(ulong guildId, ulong channelId)
|
||||||
|
{
|
||||||
|
await using var ctx = _db.GetDbContext();
|
||||||
|
var deleted = await ctx.GetTable<AutoPublishChannel>()
|
||||||
|
.DeleteAsync(x => x.GuildId == guildId && x.ChannelId == channelId);
|
||||||
|
|
||||||
|
if (deleted != 0)
|
||||||
|
{
|
||||||
|
_enabled.TryRemove(guildId, out _);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
await ctx.GetTable<AutoPublishChannel>()
|
||||||
|
.InsertOrUpdateAsync(() => new()
|
||||||
|
{
|
||||||
|
GuildId = guildId,
|
||||||
|
ChannelId = channelId,
|
||||||
|
DateAdded = DateTime.UtcNow,
|
||||||
|
},
|
||||||
|
old => new()
|
||||||
|
{
|
||||||
|
ChannelId = channelId,
|
||||||
|
DateAdded = DateTime.UtcNow,
|
||||||
|
},
|
||||||
|
() => new()
|
||||||
|
{
|
||||||
|
GuildId = guildId
|
||||||
|
});
|
||||||
|
|
||||||
|
_enabled[guildId] = channelId;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@@ -1371,4 +1371,6 @@ patronmessage:
|
|||||||
- patronmessage
|
- patronmessage
|
||||||
- patronmsg
|
- patronmsg
|
||||||
eval:
|
eval:
|
||||||
- eval
|
- eval
|
||||||
|
autopublish:
|
||||||
|
- autopublish
|
@@ -2329,4 +2329,8 @@ threadcreate:
|
|||||||
threaddelete:
|
threaddelete:
|
||||||
desc: "Delete a thread with the specified name in this channel. Case insensitive."
|
desc: "Delete a thread with the specified name in this channel. Case insensitive."
|
||||||
args:
|
args:
|
||||||
- "Q&A"
|
- "Q&A"
|
||||||
|
autopublish:
|
||||||
|
desc: "Make the bot automatically publish all messages posted in the news channel this command was executed in."
|
||||||
|
args:
|
||||||
|
- ""
|
@@ -23,6 +23,9 @@
|
|||||||
"aar_role_removed": "Users will no longer receive {0} role when they join this server.",
|
"aar_role_removed": "Users will no longer receive {0} role when they join this server.",
|
||||||
"attachments": "Attachments",
|
"attachments": "Attachments",
|
||||||
"avatar_changed": "Avatar Changed",
|
"avatar_changed": "Avatar Changed",
|
||||||
|
"autopublish_enable": "All messages posted in this channel will be automatically published.",
|
||||||
|
"autopublish_disable": "Messages in this channel will no longer be automatically published.",
|
||||||
|
"req_announcement_channel": "This command must be executed in an Announcement channel.",
|
||||||
"bandm": "You have been banned from {0} server.\nReason: {1}",
|
"bandm": "You have been banned from {0} server.\nReason: {1}",
|
||||||
"banmsg_disabled": "Ban messages are disabled. You can enable them by setting a banmsg to something other than '-'.",
|
"banmsg_disabled": "Ban messages are disabled. You can enable them by setting a banmsg to something other than '-'.",
|
||||||
"banmsg_default": "No ban message set. Default behavior will be used.",
|
"banmsg_default": "No ban message set. Default behavior will be used.",
|
||||||
|
Reference in New Issue
Block a user