feat: reset bot token button

pull/1108/head
Zomatree 2025-05-17 12:59:39 +01:00
parent bccf1eebae
commit 07d74a4ef4
No known key found for this signature in database
5 changed files with 43 additions and 7 deletions

2
external/lang vendored

@ -1 +1 @@
Subproject commit e54133252e834299a23e759b7cda2cf2b2447279
Subproject commit 4ec2f99855825743f65eabd715ec77f61c28c5d8

View File

@ -297,4 +297,5 @@ export const modalController = new ModalControllerExtended({
report_success: ReportSuccess,
modify_displayname: ModifyDisplayname,
changelog_usernames: ChangelogUsernames,
reset_bot_token: Confirmation
});

View File

@ -20,6 +20,7 @@ export default function Confirmation(
| "delete_bot"
| "block_user"
| "unfriend_user"
| "reset_bot_token"
>,
) {
const history = useHistory();
@ -31,6 +32,7 @@ export default function Confirmation(
delete_bot: ["confirm_delete", "delete"],
unfriend_user: ["unfriend_user", "remove"],
block_user: ["block_user", "block"],
reset_bot_token: ["reset_bot_token", "reset"],
};
const event = EVENTS[props.type];
@ -88,6 +90,12 @@ export default function Confirmation(
.bots.delete(props.target);
props.cb?.();
break;
case "reset_bot_token":
clientController
.getAvailableClient()
.bots
.edit(props.target.id, { remove: ["Token"] })
.then(props.callback)
}
}}
submit={{

View File

@ -190,6 +190,11 @@ export type Modal = {
type: "report_success";
user?: User;
}
| {
type: "reset_bot_token";
target: { name: string, id: string },
callback: () => Promise<void>;
}
);
export type ModalProps<T extends Modal["type"]> = Modal & { type: T } & {

View File

@ -35,6 +35,7 @@ import { modalController } from "../../../controllers/modals/ModalController";
interface Data {
_id: string;
token: string;
username: string;
public: boolean;
interactions_url?: string;
@ -74,6 +75,7 @@ function BotCard({ bot, onDelete, onUpdate }: Props) {
const [user, setUser] = useState<User>(client.users.get(bot._id)!);
const [data, setData] = useState<Data>({
_id: bot._id,
token: bot.token,
username: user.username,
public: bot.public,
interactions_url: bot.interactions_url as any,
@ -94,7 +96,7 @@ function BotCard({ bot, onDelete, onUpdate }: Props) {
const refreshProfile = useCallback(() => {
client.api
.get(`/users/${bot._id as ""}/profile`, undefined, {
headers: { "x-bot-token": bot.token },
headers: { "x-bot-token": data.token },
})
.then((profile) => setProfile(profile ?? {}));
// eslint-disable-next-line react-hooks/exhaustive-deps
@ -149,7 +151,7 @@ function BotCard({ bot, onDelete, onUpdate }: Props) {
"/users/@me",
avatar ? { avatar } : { remove: ["Avatar"] },
{
headers: { "x-bot-token": bot.token },
headers: { "x-bot-token": data.token },
},
);
@ -168,7 +170,7 @@ function BotCard({ bot, onDelete, onUpdate }: Props) {
? { profile: { background } }
: { remove: ["ProfileBackground"] },
{
headers: { "x-bot-token": bot.token },
headers: { "x-bot-token": data.token },
},
);
@ -184,7 +186,7 @@ function BotCard({ bot, onDelete, onUpdate }: Props) {
"/users/@me",
content ? { profile: { content } } : { remove: ["ProfileContent"] },
{
headers: { "x-bot-token": bot.token },
headers: { "x-bot-token": data.token },
},
);
@ -312,6 +314,7 @@ function BotCard({ bot, onDelete, onUpdate }: Props) {
if (editMode) {
setData({
_id: bot._id,
token: data.token,
username: user!.username,
public: bot.public,
interactions_url: bot.interactions_url as any,
@ -334,7 +337,7 @@ function BotCard({ bot, onDelete, onUpdate }: Props) {
<CategoryButton
account
icon={<Key size={24} />}
onClick={() => modalController.writeText(bot.token)}
onClick={() => modalController.writeText(data.token)}
description={
<>
{"••••• "}
@ -344,7 +347,7 @@ function BotCard({ bot, onDelete, onUpdate }: Props) {
ev,
modalController.push({
type: "show_token",
token: bot.token,
token: data.token,
name: user!.username,
}),
)
@ -490,6 +493,25 @@ function BotCard({ bot, onDelete, onUpdate }: Props) {
}>
<Text id="app.settings.pages.bots.add" />
</Button>
<Button
palette="error"
onClick={ async () => {
modalController.push({
type: "reset_bot_token",
target: { name: user.username, id: bot._id },
callback: async () => {
const updatedBot = await client.bots.fetch(bot._id);
setData({
...data,
token: updatedBot.bot.token
})
}
})
}
}>
<Text id="app.settings.pages.bots.reset_token" />
</Button>
</>
)}
</div>